Jump to content

Archived

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

kevdoug

Multi-Column HTML Table Results from two Database Columns

Recommended Posts

[color=green]Sorry if this topics in the wrong place but I am new to this coding.[/color]

As the subject say I am retrieving data from two database columns and would like to put it into multi columns. I can retrive it to appear in two columns no problem.

The results currently go down the page where as I would like them to go across for say six columns and then automaticaly start a new row. Is there any way of setting the coumnls at six?

I know it looks like I am not closing the <a> tag but I am it just shows up like [/url] for some strange reason.  Here is the code I am using.

[color=blue]while (OCIFetch($stmt))
      {
     
    $img = OCIResult($stmt,'IMAGE');
$titl = OCIResult($stmt,'TITLE');
echo "
<table>

<tr>
<td><a href=". $img ." target='new'><img src=' " . $img . "' height='75' width='100'></a>
</td>
</tr>

<tr>
<td>
$titl
</td>
</tr>
</table>";


      }
[/color]

Share this post


Link to post
Share on other sites
This topic is covered in the FAQ/Code sepository. [url=http://www.phpfreaks.com/forums/index.php/topic,95426.0.html]Here[/url].

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=101332.msg400980#msg400980 date=1153452850]
This topic is covered in the FAQ/Code sepository. [url=http://www.phpfreaks.com/forums/index.php/topic,95426.0.html]Here[/url].
[/quote]

As I have said I am new to PHP and coding in general I have tried following the code in the link but have had no success especially as I use Oracle database and the code is for MySQL. I have posted my code below any help would be appreciated.

[color=blue]<?php
$usrname = $_COOKIE[user_cookie];

$conn = OCILogon("kevin", "kevin", "") or die ("connection failed");
      $stmt = OCIParse($conn, "select * from product where user_name = '$usrname' ");
      OCIExecute($stmt);
      $img = OCIResult($stmt,'IMAGE');
     
if($stmt > 0)
{
    $i = 0;
    $max_columns = 3;
        while (OCIFetch($stmt))
  {
      // 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 != "" && $img != null)
          echo "<td>$img</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>";

OCIFreeStatement($stmt);
OCILogoff($conn);
}
?>[/color]

Share this post


Link to post
Share on other sites
What problem are you having with the code?

Are you getting error messages?

Is your table not printing correctly?

A little more information is needed before we can truly help... :)

Share this post


Link to post
Share on other sites
[quote author=HeyRay2 link=topic=101332.msg401221#msg401221 date=1153494471]
What problem are you having with the code?

Are you getting error messages?

Is your table not printing correctly?

A little more information is needed before we can truly help... :)
[/quote]

I am getting a blank page at the moment. The first code I posted returned the data in 2 columns down the page where I would like rows going across the page in say 6 columns and drop down for a new row.

Share this post


Link to post
Share on other sites
The code for accessing your Oracle database won't change. You're simply changing the logic that prints out the values you pulled from the database, so you will only need the basic layout of the code example you are trying to use. Like so:

[code]
<?php
$usrname = $_COOKIE[user_cookie];

$conn = OCILogon("kevin", "kevin", "") or die ("connection failed");
$stmt = OCIParse($conn, "select * from product where user_name = '$usrname' ");
OCIExecute($stmt);
       
$i = 0; // Your counter for which item in the column you are on
$max_columns = 6; // The maximum columns per row

// Start your table for your results
echo "<table>";

   // Run your while loop to start printing out your results
   while (OCIFetch($stmt))
   {

// Set your values to be printed in each "while" loop
$img = OCIResult($stmt,'IMAGE');
      $titl = OCIResult($stmt,'TITLE');

       // open row if counter is zero
       if($i == 0){
          echo "<tr>";
       }

       echo "<td>";

// This is going to be a nested table to keep your results formatted correctly
echo "
<table>
      <tr>
      <td><a href=". $img ." target='new'><img src=' " . $img . "' height='75' width='100'></a></td>
            </tr>
            <tr>
      <td>$titl</td>
            </tr>
      </table>";

// close out the cell for the nested table
echo "</td>";

       // increment counter - if counter = max columns, reset counter and close row
       if(++$i == $max_columns)
       {
           echo "</tr>";
           $i=0;
       }  // end if
   } // end while

// clean up table if we didn't fill a while row up to $max_columns - makes your code valid!
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++){
// Print some blank cells to fill up empty space
        echo "<td>&nbsp;</td>";
    }

    // Close the row if we are on the last column
    if($j == ($max_columns - 1)){
echo "</tr>";
    }
}

// Release your database results and close the connection
OCIFreeStatement($stmt);
OCILogoff($conn);

// End the table
echo "</table>";

?>
[/code]

Share this post


Link to post
Share on other sites
Cheers HeyRay2 working fine now.

I thought it was something to do with converting from MySQL to Oracle. But thats how you grow from being a newbie.

Thanks again.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

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.