Solved

dynamic table results

Posted on 2003-12-01
9
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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