Solved

dynamic table results

Posted on 2003-12-01
9
257 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
is this a cms? 8 57
PHP & MySQL - Rounding Results from a Select Query 3 25
paypal ipn to mysql 3 35
echo paypal data on screen 5 48
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…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

815 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

12 Experts available now in Live!

Get 1:1 Help Now