Jump to content

(SOLVED)nice code but how do i


just me and php

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
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.