Jump to content


Photo

(SOLVED)nice code but how do i


  • Please log in to reply
3 replies to this topic

#1 just me and php

just me and php
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 30 October 2006 - 12:50 AM

This Code Is What I Was Looking For , But How Do I Make It Display Differently
<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>

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

#2 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 30 October 2006 - 04:42 AM

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.

<?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>";
?>

Don

#3 sinisake

sinisake
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationSerbia,

Posted 30 October 2006 - 11:07 AM

This should do the work:
<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>


#4 just me and php

just me and php
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 30 October 2006 - 01:55 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users