Jump to content

Archived

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

benji87

Displaying results over columns

Recommended Posts

Hi all.

Im looking for a bit of script that will display some sql results over three columns instead of down rows. For image thumbnails.

I know someone has already put up a topic about this i tried to use the script but it was all too much for what im wanting i just want a bare bones script.

Any help would be great!

Share this post


Link to post
Share on other sites
Have you looked in the forum FAQ

http://www.phpfreaks.com/forums/index.php/topic,95426.0.html

Share this post


Link to post
Share on other sites
Thanks for the link that was really useful but its telling the that the msql_fetch_array() is an undefined function??

Share this post


Link to post
Share on other sites
This is easy connect database make a while loop get the information then format html properly and echo results.

good luck.

Share this post


Link to post
Share on other sites
Easy for who? Its only easy if you already know it. Why bother posting if your not willing to help.

Dont reply as i dont want to flame

Share this post


Link to post
Share on other sites
Thanks for the help. I did not post code coz its basicly what barand sent me the link to:

[code]<table cellspacing="3" cellpadding="3">
<?php
include ("db.php");
$query = "SELECT img_thumb FROM deck_imgs ORDER BY img_name";
$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($img_thumb != "" && $img_thumb != null)
          echo "<td>$img_name</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]

Share this post


Link to post
Share on other sites
while($row = mssql_fetch_array($result))

to

while($row = mssql_fetch_assoc($result))

Might work

Share this post


Link to post
Share on other sites
There's a typo that hasn't been noticed.

You're using mysql at the top and then mssql at the bottom... Notice the difference.  You need [color=green]m[b][color=red]y[/color][/b]sql_fetch_array[/color]

Change this:
[code=php:0]while($row = mssql_fetch_array($result))[/code]

to
[code=php:0]while($row = mysql_fetch_array($result))[/code]


Regards
Huggie

Share this post


Link to post
Share on other sites
Thanks! Its always the obvious things! It worked fine trouble is now it wont display any data!  >:(

Share this post


Link to post
Share on other sites
ok,

Try changing this:
[code=php:0]while($row = mysql_fetch_array($result))[/code]

to:
[code=php:0]while($row = mysql_fetch_array($result, MYSQL_ASSOC))[/code]


This way, extract($row) will import a variable of $img_name into the namespace, I think by default mysql_fetch_array() gets both names and numbers as an index, but by specifying MYSQL_ASSOC it gets only names, probably less overhead.

Regards
Huggie

Share this post


Link to post
Share on other sites
Hello,

I am kinda new to php. I read the above posts about how to display results over columns:

Here is my code:

<table cellspacing="3" cellpadding="3">
<?php
include ("config.php");
?>
<?php
$query = "SELECT Link,Image, Alt from pluswomens_frontimage_rotator ORDER by rand()";
$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 = mysql_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><td><img src=$Image alt=$Alt border=0 align=bottom hspace=5 vspace=5></td></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>

This is what displays:

<table cellspacing="3" cellpadding="3">
<tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

Can someone tell me what I am doing wrong, and yes there is information in pluswomens_frontimage_rotator to pull.

Share this post


Link to post
Share on other sites
This is the code that's causing problems...

[code]
// make sure we have a valid product
if($product != "" && $product != null)
  echo "<td><td><img src=$Image alt=$Alt border=0 align=bottom hspace=5 vspace=5></td></td>";
[/code]

For the time being, remove the condition and see if it works at all... Try this:

[code]
// make sure we have a valid product
// if($product != "" && $product != null) // temporary comment out
  echo <<<HTML
  <td><img src="{$Image}" alt="{$Alt}" border="0" align="bottom" hspace="5" vspace=5></td>
HTML;
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
You guys are awesome, works like a charm. I have been trying to find a way to do this for months, I guess I just had to come to the right place!!!!

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.