简体   繁体   中英

How can I arrange this code so I can get different entry from database for each table row

I want every different record of database to be display on each table rows, but I'm unable to retrieve different record for each row and column. Please give me suggestion where I can paste than while block so it will give different result for each row and column.

<?php
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = '';
  $rec_limit = 10;
  $scriptname=$_SERVER['PHP_SELF'];

  $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  if(! $conn )
  {
     die('Could not connect: ' . mysql_error());
  }
  mysql_select_db('online_shopping');   // include your code to connect to DB.

 $tbl_name="mobile_db";     //your table name
 $start = 0;
 $limit = 5; 
 $sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start,  $limit";
 $result = mysql_query($sql);
 while($row = mysql_fetch_array($result))
                        {
                            $company=$row['company'];
                            $model=$row['model'];
                            $available=$row['availability'];
                            $price=$row['price'];

                        }
 echo "<table border='2'>";
 $j = 0;
 for($j = 0; $j<5; $j++)
 {
  echo "<tr>";
  for($i = 0; $i<3; $i++)
  {
    echo "<td>";
    echo "<table border='2'>";
        echo "<tr>";
            echo "<td><img src='abc.jpg' height='250' width='250'/></td>";
        echo "</tr>";
        echo "<tr>";
            echo "<td>";
                echo "<table border='2'>";
                    echo "<tr>";
                    echo "<td><b>Brand : </b></td>";
                    echo '<td>'.$company.'</td>';
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td><b>Model : </b></td>";
                    echo '<td>'.$model.'</td>';
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td><b>Availability : </b></td>";
                    echo '<td>'.$available.'</td>';
                    echo "</tr>";
                    echo "<tr>";
                    echo "<td><b>Price : </b></td>";
                    echo '<td>'.$price.'</td>';
                    echo "</tr>";
                echo "</table>";
            echo "</td>";
        echo "</tr>";
    echo "</table>";
    echo "</td>";
    }
   echo "</tr>";
   }
  echo "</table>";
  ?>

In your while loop you always rewrite the same variables, after loop you have only last record saved.

In the loop, you have to save records into array.

In your code you have nested tables, but in the first one, there is only one row and one table cell which contains another table. I use just nested table.

<?php

...
$result = mysql_query($sql);
$data = array();

while($row = mysql_fetch_array($result)) {
    $data[] = $row;
}

if (count($data) > 0) {
    echo '<table>';
    foreach ($data as $row) {
        echo '<tr>';
            echo '<td>Brand: ' . $row['company'];
            echo '<td>Model: ' . $row['model'];
            echo '<td>Availability: ' . $row['availability'];
            echo '<td>Price: ' . $row['price'];
    }
    echo '</table>';
} else {
    echo 'no records';
}

?>

Not sure I fully understand what you are trying to accomplish but try this.

<?php
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = '';
  $rec_limit = 10;
  $scriptname=$_SERVER['PHP_SELF'];

  $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  if(! $conn )
  {
     die('Could not connect: ' . mysql_error());
  }
  mysql_select_db('online_shopping');   // include your code to connect to DB.

 $tbl_name="mobile_db";     //your table name
 $start = 0;
 $limit = 5; 
 $sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start,  $limit";
 $result = mysql_query($sql);

  echo "<table border='2'>";
  while($row = mysql_fetch_array($result))
                        {
                            echo "<tr>";
                            echo  "<td>".$row['company']."</td>";
                            echo  "<td>".$row['model']."</td>";
                            echo  "<td>".$row['availability']."</td>";
                            echo  "<td>".$row['price']."</td>";
                            echo "</tr>";

                        }
  echo "</table>";
?>

You need to move the table rows inside the fetch loop or store the row in an array . I have simplified your tables to make the example clearer:

$result = mysql_query($sql);
if (!$result) {
  /* Error */
}

echo '<table>';

while ($row = mysql_fetch_array($result)) {
  echo '<tr><td><img src="', htmlspecialchars ($row['image']), '">';
  echo '<tr><td><table>';
  echo '  <tr><th>Brand<td>',        htmlspecialchars ($row['company']);
  echo '  <tr><th>Model<td>',        htmlspecialchars ($row['model']);
  echo '  <tr><th>Availability<td>', htmlspecialchars ($row['availability']);
  echo '  <tr><th>Price<td>',        htmlspecialchars ($row['price']);
  echo '  </table>';
}

echo "</table>\n";

Some notes about the code:

  • Test the return value of mysql_query() . The query might fail.
  • Escape your output using htmlspecialchars() .
  • You should use <th> elements for your headings and style those, instead of using inline <b> elements.
  • I added output of $row['image'] which might not do what you want.
  • And do not use the deprecated mysql extension. Use PDO or mysqli instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM