i have a table with 14 columns and 4 rows. i know how to echo a result with a table from a query but how do i input results into a table thats already there? results go into cell 1, cell 2, cell 3 etc


been having trouble on this. table id= stack

its pretty long but i'll post the example so i can relate it to my code..

mysql_connect("localhost", "Master", "password;
mysql_select_db("Ustack");

$report = mysql_query("SELECT * FROM Reply ")or die (mysql_error());

while($row = mysql_fetch_array( $report)) {


	$id = $row['id'];

}

<table  border="0">
  <tr>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
    <td width="70" height="70">&nbsp;</td>
  </tr>

</table>

so how do i get $id to go to cell 1, cell 2, cell 3 etc and fill up the table. its easy when you echo it into a table but i need the table there before.

Recommended Answers

All 12 Replies

im not really following what you are trying to do if you wanna tell me with a little bit better of a explanation i can def help but i think this is what you are trying to do let me know

echo "<table border='0'><tr>";
 	for ( $counter = 0 ; $counter <= 1000; $counter += 1) {
 			echo "<td width='70' height='70'>&nbsp;$id[$counter]</td>";
	}
echo "</tr></table>";

MDanz,

You must build the table as you go, in two nested loops :-

  • Outer loop - table rows
  • Inner loop - table cells (forming colums)

On the assumption that your $report rows should each be represented as a table row, then the following php code should build the table you want :-

<?php
mysql_connect("localhost", "Master", "password;
mysql_select_db("Ustack");

$report = mysql_query("SELECT * FROM Reply ")or die (mysql_error());

echo "<table border=\"0\">\n";
while($row = mysql_fetch_array( $report)){
	echo "<tr>\n";
	foreach ($row as $value){
		echo "<td width=\"70\" height=\"70\">$value</td>\n";
	}
	echo "</tr>\n";
}
echo "</table>\n";
?>

Off-the-cuff; not tested

Airshow

heres a better question..

how do i echo 1st result, 2nd result, 3rd result? i know how to echo all of them, how do i echo the specific... e.g. 2nd result in query. or the 3rd result in the query

$addz = mysql_query("SELECT * FROM Stacks WHERE username = 'Admin'")or die (mysql_error());

while($rowrun = mysql_fetch_array( $addz)) {
			  
 $idz = $rowrun['id'];
			  
			  
			  }

how do i echo 1st result, 2nd result, 3rd result? i know how to echo all of them, how do i echo the specific... e.g. 2nd result in query. or the 3rd result in the query

This doesn't make sense.

Your query yields only one result ($result), which is (in all probability) "2-dimensional"; an array of arrays. The top level array should be indexed by integer ([1], [2], [3], [4]), while the level-2 arrays should be indexed by column name (as established in the database).

Therefore any particular item of data should be addressable as $result[n]. This is effectively what happens (via different syntax) in my nested loops example above.

Airshow

Member Avatar for diafol

You should use the LIMIT keyword in the sql query.

SELECT ... FROM ... WHERE .... ORDER BY .... LIMIT(x,y)

The WHERE and ORDER BY clauses are optional
LIMIT has two parameters (both integers)
The first integer is the offset - i.e. start returning records from record number (x) of the query - the first record is 0 - not 1!
The second integer (y) will limit the resulting recordset to that number of records.

To return the only 2nd record, just do: LIMIT(1,1)
To return the 2nd and 3rd record, do: LIMIT(1,2)
To return the first 10 records, do: LIMIT(0,10)

I don't know if this is what you're looking for.

while($row = mysql_fetch_array( $report)) {


	$id = $row['id'];

}

This won't work, by the way. You'll end up with only the $row of the last row of your results. You're not preserving the previous values. To do that, either $id would need to be an array, or you'd need to build the table within the while loop.

As for having the table already there and then later inserting the data, that sounds more like something you'd need JavaScript or AJAX to do -- though I'm not sure if that's even possible, as I'm no expert in either.

the first record is 0 - not 1!

That's a good point Ardav, which alerts me to an error in my post above.

My " [1], [2], [3], [4] " should read " [0], [1], [2], [3] "

Airshow

As for having the table already there and then later inserting the data, that sounds more like something you'd need JavaScript or AJAX to do -- though I'm not sure if that's even possible, as I'm no expert in either.

That's exactly what I thought Evo but I sort of ignored it because the question is posed in the PHP forum.

It's certainly possible to replace the contents of individual table cells using AJAX/DHTML methods in Javascript. As far as I can tell, that does not apply here but I may be wrong.

Airshow

That's exactly what I thought Evo but I sort of ignored it because the question is posed in the PHP forum.

It's certainly possible to replace the contents of individual table cells using AJAX/DHTML methods in Javascript. As far as I can tell, that does not apply here but I may be wrong.

I thought I'd mention it in case the OP didn't know it was an option, since PHP certainly cannot do what he/she is asking for =)

ok i've got it working.. can you help me get my pagination working along with it.. i get the Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ustackc1/public_html/test2.php on line 30

<?php

$rows = 4;
$cols = 14;

mysql_connect("localhost", "Master", "password");
mysql_select_db("Ustack");

$maxCount = $rows * $cols;
 $construct= "SELECT * FROM Stacks Where username='Admin' LIMIT 0, {$maxCount}";
   
$limit = 52;
   $page = $_GET['page'];

       if($page){
          $start = ($page - 1) * $limit;
       }else{
          $start = 0;
       }


$foundnum = mysql_num_rows(mysql_query($construct));
$construct = $construct.' LIMIT '.$start.' , '.$limit;
$run = mysql_query($construct);
    
$idList = array();

while($row = mysql_fetch_assoc($result))
{
    $idList[] = $row['id'];
	


$tableOutput = '';

for($row=0; $row<$rows; $row++)
{
    $tableOutput .= "  <tr>\n";
    for($col=0; $col<$cols; $col++)
    {
        $idIndex = ($row * $cols) + $col;

        $tableOutput .= "<td>";
        $tableOutput .= (isset($idList[$idIndex]) ? $idList[$idIndex] : '&nbsp;');
        $tableOutput .= "</td>\n";
    }
    $tableOutput .= "  </tr>\n";
}}
echo "<table border='0'>
  $tableOutput 
</table>";
// How many adjacent pages should be shown on each side?
       $adjacents = 3;
           
        // Your file name  (the name of this file)
       $targetpage = "test2.php";

       if ($page == 0){
          $page = 1;
       }

       $prev = $page - 1;

       $next = $page + 1;

       $lastpage = ceil($foundnum/$limit);

       $lpm1 = $lastpage - 1;

       $pagination = "";
       if($lastpage > 1){
          $pagination .= "<div class=\"pagination\">";
          // Previous
          if ($page > 1){
             $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$prev\" style='text-decoration: none';> <strong>&laquo; previous</strong></a>";
          }else{
             $pagination .= "<span class=\"disabled\"> <strong>&laquo; previous</strong></span>";
          }

          // Pages

          if ($lastpage < (7 + ($adjacents * 2))){   
             for ($counter = 1; $counter <= $lastpage; $counter++){
                if ($counter == $page){
                   $pagination .= "<span class=\"current\"><strong> $counter</strong></span>";
                }else{
                   $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\" style='text-decoration: none';><strong> $counter</strong></a>";
                }
             }
          }elseif($lastpage > (5 + ($adjacents * 2))){
             if($page < 1 + ($adjacents * 2)){
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                   if ($counter == $page){
                      $pagination .= "<span class=\"current\"><strong> $counter</strong></span>";
                   }else{
                      $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\" style='text-decoration: none';><strong> $counter</strong></a>";
                   }
                }
             }

          $pagination .= "...";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lpm1\" style='text-decoration: none';><strong> $lpm1</strong></a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lastpage\" style='text-decoration: none';><strong> $lastpage</strong></a>";
       }elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)){
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=1\" style='text-decoration: none';><strong> 1</strong></a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=2\" style='text-decoration: none';><strong> 2</strong></a>";
          $pagination .= "...";

          for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++){
             if ($counter == $page){
                $pagination .= "<span class=\"current\"><strong> $counter</strong></span>";
             }else{
                $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\" style='text-decoration: none';><strong> $counter</strong></a>";
             }
          }
          $pagination .= "...";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lpm1\" style='text-decoration: none';><strong> $lpm1</strong></a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$lastpage\" style='text-decoration: none';><strong> $lastpage</strong></a>";
        }else{
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=1\" style='text-decoration: none';> 1</strong></a>";
          $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=2\" style='text-decoration: none';> 2</strong></a>";
          $pagination .= "...";

          for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++){
             if ($counter == $page){
                $pagination .= "<span class=\"current\"><strong> $counter</strong></span>";
             }else{
                $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$counter\" style='text-decoration: none';><strong> $counter</strong></a>";
             }
          }
       }
               
       // Next
       if ($page < $counter - 1){
           $pagination .= "<a href=\"$targetpage?search=".$_GET['search']."&page=$next\" style='text-decoration: none';><strong> next &raquo;</strong></a>";
       }else{
          $pagination .= "<span class=\"disabled\"><strong> next &raquo;</strong></span>";
          $pagination .= "</div>\n";
        }
       }
      echo $pagination;
   







?>

You're passing $result to mysql_fetch_assoc() . You should be passing $run to it instead.

Also, you might want to note that on line 10, your code says

$construct= "SELECT * FROM Stacks Where username='Admin' LIMIT 0, {$maxCount}";

Then on line 23 you concatenate this as follows:

$construct = $construct.' LIMIT '.$start.' , '.$limit;

So, you end up with a sql query that looks like this:

SELECT * FROM Stacks Where username='Admin' LIMIT 0, {$maxCount} LIMIT <start's value>, 52

So you've got LIMIT twice in one query...

I thought I'd mention it in case the OP didn't know it was an option, since PHP certainly cannot do what he/she is asking for =)

Evo, we are clearly of one mind here. :cool:

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.