Can't seem to figure this out... HTML Table and DB info

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Can't seem to figure this out... HTML Table and DB info

Post by Rasta »

Hey there,

I'm trying to set up a page that gets an array of different company information from my database. Each one is returned in an array $row.

I'm trying to put the information into an HTML table that has a table with one row and two columns. I want info for the first company to appear in the first column (this includes a 2x2 table that has name, info, pic, and link). The second column has the same thing, but for the following company.... and so on and so forth for as many arrays of co. info is fetched from the DB.

Here's my code:

Code: Select all

//HERE'S A ONE ROW TABLE W/  TWO COLUMNS
echo'<table width="750">
                 <tr> 
                   <td>nbsp;</td>
                   <td>nbsp;</td>
                 <tr>
         </table>';

 $sql = "select * from content
	where pageid = '$pageid'
 	order by contentid 
	";

 $sqlresult = mysql_query ($sql, $db);

//I WANT THE FIRST RETURN OF $row TO GO IN THE FIRST COLUMN
//I WANT THE SECOND RETURN OF $row TO GO IN THE 2ND COLUMN
//THEN CREATE A NEW ROW IN THE HTML TABLE, AND DO IT ALL AGAIN

 while ($row= mysql_fetch_array($sqlresult))&#123;
 

//I'M CREATING A FOUR SQUARE TABLE FOR THE INFO TO APPEAR IN W/IN COLUMN 1
 
//THIS FOLLOWING LINE PRINTS OUT THE COMPANY NAME 
  echo '<tr>
            <td>
              $row&#1111;3]
             </td>';
 
//THIS SHOWS THE COMPANY TEXT 
 echo '<td>
           $row&#1111;5]
         </td>
          <td></td>
          <td>&nbsp;</td><tr>
                 <td>';
 
//THIS ADDS IN THE COMPANY PICTURE
 echo '<img src="../images/'.$row&#1111;8].'.jpg">';
  
 echo '</td></tr></table>';
&#125;
Basically the return I get is each block of Company info in a 2x2 table that appears in the first column running down the page.

I think what would help me most would be if I new how to increment through returns of $row. For example, put first instance of $row in this table, then put the second instance of $row in the next.

Any help would be greatly appreciated. I've been stuck on this for a couple days now, and would love to figure it out.

Thanks
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

for or while loops

Post by phpScott »

the best way I know it to use a for or while loop and increment the $row[x] value as you go just substituting the values into your table.

Code: Select all

for($x=0; $x<$numRows; $x+=2)
&#123;
     echo"<tr><td>".$row&#1111;$x]."</td><td>".$row&#1111;$x+1]."</td></tr>";
&#125;
will create on row two cells wide and will keep repeating until you run out of rows.

phpScott
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

An alternative way (untested)

Code: Select all

echo "<table>";
while($row = mysql_fetch_array($result))&#123;
echo "<tr>";
foreach($row as $key => $val)
 echo "<td>$val</td>";
echo "</tr>";
&#125;
echo "</table>";
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

Ehh, I didn't read careful enough. Think it's time to go home and sleep! :oops:

But maybe you can use my code to something :?
Rasta
Forum Newbie
Posts: 11
Joined: Thu Aug 15, 2002 3:50 pm

Post by Rasta »

Hey Haagen, and PHPScott,

Thanks for the replys. I did get it figured out not too late last night.

I'm not sure if it's the most concise or pretty code, but it works.

Thanks for the help.

Here's what I came up with:

Code: Select all

$sql = "select * from content
	where pageid = '$pageid'
 	order by contentid 
	";

 $sqlresult = mysql_query ($sql, $db);


 $i=0;
 while ($row= mysql_fetch_array($sqlresult))&#123;
 	
 //THIS CREATES THE FIRST CELL OF A TABLE, AND DROPS IN A TWO ROW TABLE WITHIN IT
  if ($i==0)&#123;
  echo '<tr>
	<td height="67" align="left" valign="top"><table width="200" height="115" border="0">
          <tr>
            <td height="32">';
  echo '<a href ="'.$row&#1111;6].'" target = "_blank">';
  echo stripslashes($row&#1111;3]);
  echo '</a></td>';
  echo '</tr>
        <tr>
	<td height="52">';
  //PUT IN THE IMAGE IF THERE IS ONE
         if($row&#1111;8]!='')&#123;
  echo '<img src="../images/'.$row&#1111;8].'.jpg" >';
	&#125;
  echo '</td>
	 </tr> 
	  </table></td>';


  //THIS PUTS THE TEXT "SITES" IN THE NEXT COLUMN OVER
  echo '<td align="left" valign="top">';
   if($row&#1111;5]!='')&#123;
   echo '<p>';
   echo stripslashes($row&#1111;5]);
   echo '</p>';
	&#125;
  echo '</td>
  <td height="67" align="left" valign="top">&nbsp;</td>';
 $i=2;
  &#125;


//THIS CREATES THE SECOND CELL, AND PUTS IN THE APPROPRIATE INFO, THEN IT CLOSES THE ROW. 
 if ($i==1)&#123;

    echo '<td><table>
          <tr>
            <td height="32">';
  echo '<a href ="'.$row&#1111;6].'" target = "_blank">';
  echo stripslashes($row&#1111;3]);
  echo '</a></td>';
  echo '</tr>
        <tr>
	<td height="52">';
  //PUT IN THE IMAGE IF THERE IS ONE
         if($row&#1111;8]!='')&#123;
  echo '<img src="../images/'.$row&#1111;8].'.jpg">';
	&#125;
  echo '</td>
	 </tr>
	  </table></td>';

  //THIS PUTS THE TEXT "SITES" IN THE NEXT COLUMN OVER
  echo '<td align="left" valign="top">';
   if($row&#1111;5]!='')&#123;
   echo '<p>';
   echo stripslashes($row&#1111;5]);
   echo '</p>';
	&#125;
  echo '</td>
          </tr>
	 ';
    	&#125;

  if ($i==2)&#123;
	$i=1;
  &#125; else &#123;
	$i=0;
  &#125;
  &#125;
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

pretty and elegant

Post by phpScott »

my motto is if it works like you want GREAT!!
pretty and elegant can come latter when you get more experience and revisit the script latter.

phpScott
Post Reply