Solved

dynamic table results

Posted on 2003-12-01
9
242 Views
Last Modified: 2008-03-17
hello,
I am trying to find a tutorail to create a dynamic table, what i mean is this, if their are 3 results it will output like this
<tr><td>$output</td><td>$output</td><td>$output</td></tr>

is their is only one then it would be this
<tr><td>$output</td><td>&nbsp;</td><td>&nbsp;</td></tr>

is their is 5 then it would be this
<tr><td>$output</td><td>$output</td><td>$output</td></tr>
<tr><td>$output</td><td>$output</td><td>&nbsp;</td></tr>

I have no idea how to do this with php, i have an sap script, but i cant see using it,, this is my output script for displaying single rows
            <?
while($row = @mysql_fetch_array($result))
{

     $imageID = $row['imageID'];
     $imageName = $row['imageName'];
     $imageNameS = $row['imageNameS'];       
     $imageClientID = $row['imageClientID'];
       $imageBlurb = $row['imageBlurb'];
       $imageDate = $row['imageDate'];

$option_block .="<td><a href='admin/uploads/$imageName' target='blank'><img src='$imageNameS' height='100'></a></td>";

}
$display_block = "
<table><tr>
 $option_block  
  </tr></table>";
?>
0
Comment
Question by:jblayney
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:jblayney
ID: 9854998
ok, i tried this,i think im close.. no errors , but no output either

            <?
while($row = @mysql_fetch_array($result))
{

     $imageID = $row['imageID'];
     $imageName = $row['imageName'];
     $imageNameS = $row['imageNameS'];       
     $imageClientID = $row['imageClientID'];
       $imageBlurb = $row['imageBlurb'];
       $imageDate = $row['imageDate'];


$i = $row;
while ($i < 3) {
$option_block .="<tr><td><a href='admin/uploads/$imageName' target='blank'><img src='$imageNameS' height='100'></a></td>";
$i++;
$option_block .="</tr>";
}
$option_block .="</tr>";
}



$display_block = "
<table>
 $option_block  
</table>";
?>
0
 
LVL 1

Author Comment

by:jblayney
ID: 9855283
ok, im even closer, my output now diplays an error and each image is displayed 3 times.. lol

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /u/j/jeremy/www.website.com/photopage.php on line 119
 
is my error

while($row = @mysql_fetch_array($result))
{

     $imageID = $row['imageID'];
     $imageName = $row['imageName'];
     $imageNameS = $row['imageNameS'];       
     $imageClientID = $row['imageClientID'];
       $imageBlurb = $row['imageBlurb'];
       $imageDate = $row['imageDate'];

$num_rows = mysql_num_rows($sql);
$i = $num_rows;
while ($i < 3) {
$option_block .="<td><a href='admin/uploads/$imageName' target='blank'><img src='admin/uploads/$imageNameS' height='100'></a></td>";
$i++;
$option_block .="</tr>";
}
$option_block .="</tr>";
}
$display_block = "
<table>
 $option_block  
</table>";
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9855375
hi,

i thought i'd try and do this as cleanly as possible:

<?
$j = 0;
print "<table>";
for($i = 0; $row = mysql_fetch_array($result) > $i; $i++) {
  if($j == 0) {
    print "<tr>";
  }
  print "<td><a href=\"admin/uploads/" . $row["imageName"] . "\" target='blank'><img src=\"" . $row["imageNameS] . "\" height=\"100\"></a></td>";
  if($j == 2) {
    print "</tr>";
    $j = -1;
  }
$j++;
}
$left = 3 - $j;
$k = 0;
if($left > 0) {
  for($k = 0; $k < $left; $k++) {
    print "<td>&nbsp;</td>\n";
  }
}
print "</table>";
?>

tell me how you get on

loz
0
 
LVL 1

Author Comment

by:jblayney
ID: 9856123
tx loz,
it is clean, one thign doesnt work though

print "<td><a href=\"admin/uploads/" . $row["imageName"] . "\" target='blank'><img src=\"admin/uploads/" . $row["imageNameS"] . "\" height=\"100\"></a></td>";

its displaying empty images and the links dont work, when you roll over with the mouse it displays the file path minus the filename
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:jblayney
ID: 9856186
hey loz,
i rewrote the output 3 different ways, it all does the same thing, it gets the proper amount of results and displayes the tr and td properly, but its not getting the imageName and imageNameS,

Its like its this
I know their are 4 of you, but i dont know your names
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9856199
hmm that's strange can you do print_r($row); after each for statement and paste the html in here? (i.e. run the script > view source then copy the output and paste it here)

cheers,

loz
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9856209
well that leads me to suspect something in your query is amiss - can i see it? maybe you're not selecting the correct columns?

loz
0
 
LVL 1

Author Comment

by:jblayney
ID: 9856293
ok, my SQL was select * but now i specified

$sql = "SELECT imageID, imageName, imageNameS FROM $table_name WHERE imageClientID = $id ORDER BY imageName";
            
$result = @mysql_query($sql, $connection) or die("Error3 - Couldn't connect - please try later.");

$j = 0;
print "<table>";
for($i = 0; $row = @mysql_fetch_array($result) > $i; $i++) {
print_r($row);
if($j == 0) {
print "<tr>";
}
print "<td><a href=\"admin/uploads/" .$row["imageName"]. "\" target='blank'><img src=\"admin/uploads/" .$row["imageNameS"]. "\" height=\"100\"></a></td>";


if($j == 2) {
print "</tr>";
$j = -1;
}
$j++;
}
$left = 3 - $j;
$k = 0;
if($left > 0) {
for($k = 0; $k < $left; $k++) {
print_r($row);
print "<td>&nbsp;</td>\n";
}
}
print "</table>";
?>

I added 2 print_r($row); and my HTML output is

<table>1<tr><td><a href="admin/uploads/" target='blank'><img src="admin/uploads/" height="100"></a></td>1<td><a href="admin/uploads/" target='blank'><img src="admin/uploads/" height="100"></a></td>1<td><a href="admin/uploads/" target='blank'><img src="admin/uploads/" height="100"></a></td></tr>1<tr><td><a href="admin/uploads/" target='blank'><img src="admin/uploads/" height="100"></a></td><td>&nbsp;</td>
<td>&nbsp;</td>
</table>
0
 
LVL 13

Accepted Solution

by:
lozloz earned 50 total points
ID: 9857516
try this:

<?
$j = 0;
print "<table>";
$num = mysql_num_rows($result);
for($i = 0; $num > $i; $i++) {
  $row = mysql_fetch_assoc($result);
  if($j == 0) {
    print "<tr>";
  }
  print "<td><a href=\"admin/uploads/" . $row["imageName"] . "\" target='blank'><img src=\"" . $row["imageNameS] . "\" height=\"100\"></a></td>";
  if($j == 2) {
    print "</tr>";
    $j = -1;
  }
$j++;
}
$left = 3 - $j;
$k = 0;
if($left > 0) {
  for($k = 0; $k < $left; $k++) {
    print "<td>&nbsp;</td>\n";
  }
}
print "</table>";
?>

loz
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now