Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 29th, 2004, 10:24 AM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default write out row data in columns

Consider the MySQL table with

id | image | name | description
(values follow)
1 | image1 | name1 | descript1
2 | image2 | name2 | descript2
3 | image3 | name3 | descript3
4 | image4 | name4 | descript4
.
.
.
X | imageX | nameX | descript


How does one massage the data so that it can be written out in an HTML table in rows like this:

image1 image2 image3
name1 name2 name3
descript1 descript2 descript3

(next set of three in next row)

image4 image 5 image6
name4 name5 name6
descript4 name5 name6

(next set of three in next row) and so on
thanks!
bill
 
Old July 29th, 2004, 04:35 PM
Authorized User
 
Join Date: Jun 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hmmm.... what if you fetch the data from columns in the table and insert the data into arrays, ie get the whole image column into image array, name column into name array and description column into description array, then write it out three sets at a time, first numbers 1, 2 and 3 for image, then for name and then for description, and so on.
i seem to be recommending loops a lot these days, but i think your case could benefit from using a loop.

$rows = numberOfRowsInTheTableDividedByThree;

for($counter = 1; $counter <= $rows; $counter + 3)
  {
  $left = $counter;
  $middle = $counter + 1;
  $right = $counter + 2;

  echo "<tr>"
  echo "<td>" . $image[$left] . "</td>";
  echo "<td>" . $image[$middle] . "</td>";
  echo "<td>" . $image[$righ] . "</td>";
  echo "</tr>"
  echo "<tr>"
  echo "<td>" . $name[$left] . "</td>";
  echo "<td>" . $name[$middle] . "</td>";
  echo "<td>" . $name[$right] . "</td>";
  echo "</tr>"
  echo "<tr>"
  echo "<td>" . $description[$left] . "</td>";
  echo "<td>" . $description[$middle] . "</td>";
  echo "<td>" . $description[$right] . "</td>";
  echo "</tr>";
  }


unless i've really misunderstood you, this should be what you're looking for.
now all you need to do is pour it into a bowl, add a <table> tag and </table>, mix in some MySQL queries and bake for a few moments.
hope you like it!


grimmy
God, grant me the serenity to accept the things I cannot change, the courage to change the things I can, and the wisdom to hide the bodies of those people I had to kill because they pissed me off.
 
Old October 3rd, 2004, 12:09 PM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've tried the code suggested but have not had complete success. Here is the code I've attempted with an explanation of the results following the code:

<?php

               $sql = "SELECT * FROM products
                 WHERE product_type=\"Candle\"";

               $sql_result2 = mysql_query($sql)
                       or die("Couldn't execute query.");

               if (!$sql_result2) {

                       echo "<P>Couldn't get list!";

               } else {


$num2=mysql_numrows($sql_result2);
$rows= (($num2)/3);


       while ($row = mysql_fetch_array($sql_result2)) {
               $product_image = $row["product_image"];
               $product_name = $row["product_name"];
               $item_no = $row["item_no"];
               $product_description = $row["product_description"];

echo "<table>
<tr><td class=\"tbltxt\">";
for($counter = 1; $counter <= $rows; $counter++)
  {
  $left = $counter;
  $middle = $counter + 1;
  $right = $counter + 2;

   echo "<tr>";
 echo "<td><img src='$product_image'>[$left]</td>";
  echo "<td><img src='$product_image'>[$middle]</td>";
  echo "<td><img src='$product_image'>[$right]</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>" . $product_name[$left] . "</td>";
  echo "<td>" . $product_name[$middle] . "</td>";
  echo "<td>" . $product_name[$right] . "</td>";
  echo "</tr>";
  echo "<tr>";
  echo "<td>" . $product_description[$left] . "</td>";
  echo "<td>" . $product_description[$middle] . "</td>";
  echo "<td>" . $product_description[$right] . "</td>";
  echo "</tr>";
  }
echo "</table>";
}
}
?>
*********************

what the above code produces is the following:

FIRST ROW OUTPUT:

image1 | 2nd letter of name | 2nd letter description
image1 | 3rd letter of name | 3rd letter description
image1 | 4th letter of name | 4th letter description

SECOND ROW OUTPUT:

image1 | 3rd letter of name | 3rd letter description
image1 | 4th letter of name | 4th letter description
image1 | 5th letter of name | 5th letter description

THIRD ROW OF OUTPUT:

image2 | 2nd letter of name | 2nd letter description
image2 | 3rd letter of name | 3rd letter description
image2 | 4th letter of name | 4th letter description

FOURTH ROW OF OUTPUT:

image2 | 3rd letter of name | 3rd letter description
image2 | 4th letter of name | 4th letter description
image2 | 5th letter of name | 5th letter description

In other words, the 3-across output works but each image is output 6 times (2 rows X 3 images for each image) and instead of the names and descriptions being output in whole, only letters from the names and descriptions are being output.

Any help would be appreciated.
Thanks,
bill

 
Old October 4th, 2004, 12:05 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would suggest three different selects with the data in exactly the same order. Thus you loop through one result set for three pictures, then the other result set for the names, etc. But you must keep them in perfect sync. Or loop through one result set then move back -3 rows, get the names. moving back three rows once for each column in your result set.

 
Old October 7th, 2004, 09:17 AM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your comments. Was able to accomplish the task this way:

<?php
               $sql = "SELECT * FROM products
                 WHERE product_type=\"$product_type\"";

               $sql_result = mysql_query($sql)
                       or die("Couldn't execute query.");

               if (!$sql_result) {

                       echo "<P>Couldn't get list!";

               } else {


$num=mysql_numrows($sql_result);


if ($num==0)
{
echo "<table>
<tr><td>";
echo "<center>Current Products</center>

<p>No Products Were Retrieved</td></tr></table>";
}
else
{

echo "<table>
<tr><td><b><center>Current Products</center>
</tr></td><tr>";

      $i = 1;

      while ($row = mysql_fetch_assoc($sql_result)) {

          echo "<td><img src=". $row['product_image'] . ">";
         echo "<br>" . $row['item_no'] . "";
         echo "<br>" . $row['product_name'] . "";
         echo "<br>". $row['product_description'] ."</td>\n";
           if (($i++ % 3) == false) {
              echo "</tr>\n<tr>\n";
          }
      }
echo "</tr></table>";
}}
?>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Totaling columns in a footer row of a GridView psimonson ASP.NET 2.0 Basics 1 May 5th, 2008 01:34 PM
certain columns of a row in datagrid visible false nanu yaru VS.NET 2002/2003 0 March 31st, 2008 08:27 AM
show columns with same ID in the same row yuvalk SQL Server 2005 4 February 2nd, 2007 07:46 PM
Converting large columns into a single row rahulpokharna SQL Server 2000 3 January 10th, 2006 02:14 AM
Manage data row by row in datagrid Dragonist Classic ASP Databases 5 July 29th, 2004 04:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.