Jump to content

Archived

This topic is now archived and is closed to further replies.

just me and php

(SOLVED)nice code but how do i

Recommended Posts

This Code Is What I Was Looking For , But How Do I Make It Display Differently
[code]<table cellspacing="3" cellpadding="3">
<?php
$query = "SELECT product FROM selling_items ORDER BY prod_id";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
if($result && mysql_num_rows($result) > 0)
{
    $i = 0;
    $max_columns = 3;
    while($row = mssql_fetch_array($result))       
   {
       // make the variables easy to deal with
       extract($row);

       // open row if counter is zero
       if($i == 0)
          echo "<tr>";

       // make sure we have a valid product
       if($product != "" && $product != null)
          echo "<td>$product</td>";
   
       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns)
       {
           echo "</tr>";
           $i=0;
       }  // end if
   } // end while
} // end if results

// clean up table - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td>&nbsp;</td>";
}
?>
</tr>
</table>
[/code]

It Displays Like This

1  2  3
4  5  6
7  8  9
10 11

And I Would Like It To Display Like This

1  5  9
2  6  10
3  7  11
4  8

Share this post


Link to post
Share on other sites
I think you'll have to rewrite it.  You can write the code to retrieve the data into an array.  Here's a way to do what you're asking for after the data is in the array.

[code]<?php
$maxrows = 4;
//build an array for testing purposes
for ($i=0;$i<21;$i++){
  $arr[$i]=$i;
}
$k = 0;
$columns = count($arr)/$maxrows;
echo "<table>";
for($i=0;$i<$maxrows;$i++){
  $row="<tr>";
  for($j=0;$j<$columns;$j++){
    if($k<count($arr)){
      $row .= "<td>" . $arr[$k] . "</td>";
      $k++;
    }
  }
  echo $row . "</tr>\n";
}
echo "</table>";
?>
[/code]

Share this post


Link to post
Share on other sites
This should do the work:
[code]
<table border='1'><tr>
<?php
$host='localhost';
$dbuser='';
$dbpass='';
$database='';
$link = mysql_connect($host, $dbuser,$dbpass)
or die ("Could not connect to MySQL");
mysql_select_db ($database)
or die ("Could not select database");
$query = "SELECT * FROM products";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
$i = 0;
$max_rows = 4;
$num_rows=mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$i++;
$br++;
if($i == 1){
echo "<td valign='top'>\n";
echo "<table>\n";
}
if($i<=$max_rows){
echo "<tr><td>$row[ID]</td></tr>\n";
}
if($i==$max_rows||$br==$num_rows){
echo "</table>\n</td>\n";
$i=0;
}
}
?>
</tr></table>
[/code]

Share this post


Link to post
Share on other sites
thanks doni49 For Your Help , But I Used  sinisake Code And Mod It To What I Was Looking For And It Worked Great :)
Thanks Again Guys For Your Help Here And Thanks For This Forum

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.