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

suhas

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>";
  ?>
Nisse Engström

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get the row count of a table that is in a different database from where the query is run?

From Dev

How can I get the UIButton state to be different for each tableView row?

From Dev

C# - How can i get header row from datatable and arrange it vertically down a column?

From Dev

how can I get the next row from the database?

From Dev

How can I get data from database into HTML table?

From Dev

How can I get data from a MySQL database with Code Igniter?

From Dev

How can I modify this code so that each table cell can be deleted?

From Dev

How can I get the "row" from indexPathsForSelectedRows?

From Dev

How can I get my C# and MySql to return more than one entry from the database?

From Dev

How can I find the next entry in a table after an entry in a different table?

From Dev

How can i add a button on each row in a table in CODENAME ONE?

From Dev

How can I insert row for each existing id into same table?

From Dev

How can I drag and drop each row in this table?

From Dev

How can I apply a procedure on each row of a table in PostgreSQL (not a trigger)

From Dev

How can I delete a single entry from a table

From Dev

How can I delete a single entry from a table

From Dev

How can I get data from a Table

From Dev

How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview

From Dev

RecyclerView - how can I get a reference to each row's View

From Dev

RecyclerView - how can I get a reference to each row's View

From Dev

How I can get the first row of each hour?

From Dev

How can I get the tkinter entry from a loop

From Dev

How can I get another HasMap from entry.getValue?

From Dev

From an IP how can I get a DNS entry?

From Dev

How can I get the tkinter entry from a loop

From Dev

How can i use a lookup table to get a row from a parent table using parameter in django

From Dev

How can I arrange (different) squares in a grid correctly?

From Dev

How can I create a table with different fields from different events?

From Dev

How can I get a different hover event for each word of a sentence?

Related Related

  1. 1

    How can I get the row count of a table that is in a different database from where the query is run?

  2. 2

    How can I get the UIButton state to be different for each tableView row?

  3. 3

    C# - How can i get header row from datatable and arrange it vertically down a column?

  4. 4

    how can I get the next row from the database?

  5. 5

    How can I get data from database into HTML table?

  6. 6

    How can I get data from a MySQL database with Code Igniter?

  7. 7

    How can I modify this code so that each table cell can be deleted?

  8. 8

    How can I get the "row" from indexPathsForSelectedRows?

  9. 9

    How can I get my C# and MySql to return more than one entry from the database?

  10. 10

    How can I find the next entry in a table after an entry in a different table?

  11. 11

    How can i add a button on each row in a table in CODENAME ONE?

  12. 12

    How can I insert row for each existing id into same table?

  13. 13

    How can I drag and drop each row in this table?

  14. 14

    How can I apply a procedure on each row of a table in PostgreSQL (not a trigger)

  15. 15

    How can I delete a single entry from a table

  16. 16

    How can I delete a single entry from a table

  17. 17

    How can I get data from a Table

  18. 18

    How can I get the row id of the a table in the sqlite database when an Item is clicked in the listview

  19. 19

    RecyclerView - how can I get a reference to each row's View

  20. 20

    RecyclerView - how can I get a reference to each row's View

  21. 21

    How I can get the first row of each hour?

  22. 22

    How can I get the tkinter entry from a loop

  23. 23

    How can I get another HasMap from entry.getValue?

  24. 24

    From an IP how can I get a DNS entry?

  25. 25

    How can I get the tkinter entry from a loop

  26. 26

    How can i use a lookup table to get a row from a parent table using parameter in django

  27. 27

    How can I arrange (different) squares in a grid correctly?

  28. 28

    How can I create a table with different fields from different events?

  29. 29

    How can I get a different hover event for each word of a sentence?

HotTag

Archive