Jump to content

Displaying results over columns


benji87

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!
Link to comment
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]
Link to comment
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
Link to comment
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
Link to comment
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.
Link to comment
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
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.