Solved

dynamic table results

Posted on 2003-12-01
9
252 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

864 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

20 Experts available now in Live!

Get 1:1 Help Now