Jump to content


Photo

Multi-Column HTML Table Results from two Database Columns


  • This topic is locked This topic is locked
6 replies to this topic

#1 kevdoug

kevdoug
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 21 July 2006 - 03:30 AM

Sorry if this topics in the wrong place but I am new to this coding.

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.

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


      }


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 July 2006 - 03:34 AM

This topic is covered in the FAQ/Code sepository. Here.

#3 kevdoug

kevdoug
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 21 July 2006 - 02:38 PM

This topic is covered in the FAQ/Code sepository. Here.


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.

<?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);
}
?>


#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 21 July 2006 - 03:07 PM

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... :)

#5 kevdoug

kevdoug
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 21 July 2006 - 03:15 PM

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... :)


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.

#6 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 21 July 2006 - 03:48 PM

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:

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

?>



#7 kevdoug

kevdoug
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 22 July 2006 - 11:39 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users