Page 1 of 2

Horizontal Repeat Loop

Posted: Wed Oct 15, 2003 7:10 am
by loco_ola
Hello all,

I have a recordset which i want to populate a table. I want there to be 3 colums accross and as many rows down as necessary.

I can loop down the row, but cant work out how i can loop horizontally only three times then go to the next row and repeat. I guess i have to use an array, but im pretty new to php....

Any help anyone can offer would be greatly appreciated.

Many Thanks
Oli
8O

The Code for my recordset is:

Code: Select all

mysql_select_db($database_conwildtangent, $conwildtangent); 
$query_rsservicetext = "SELECT tblprojects.projectid, tblprojects.projectname, tblprojects.projecticon, tblprojects.projecttext, tblservicedata.servicedataid, tblprojectassociations.projectassociationid, tblprojectassociations.projectid, tblprojectassociations.servicetypeid FROM tblprojects INNER JOIN (tblprojectassociations INNER JOIN tblservicedata ON tblprojectassociations.servicetypeid=tblservicedata.servicedataid) ON tblprojectassociations.projectid=tblprojects.projectid WHERE tblservicedata.servicetypeid=5";
And the table cell i wish to create will look like this:

Code: Select all

<td width="33%" align="center"top"><h6><a href="projects.php?projectid=<?php echo $row_rsservicetext&#1111;'projectid']; ?>"><img src="images/<?php echo $row_rsservicetext&#1111;'projecticon']; ?>" alt="<?php echo $row_rsservicetext&#1111;'projecticon']; ?>" name="icon_3dvis" border="0" id="icon_3dvis"><strong><br> 
</strong></a><a href="projects.php?projectid=<?php echo $row_rsservicetext&#1111;'projectid']; ?>"><strong><?php echo $row_rsservicetext&#1111;'projectname']; ?></strong></a></h6> </td>

Posted: Wed Oct 15, 2003 9:25 am
by choppsta
I would get all the data for each cell into an array, one element for each cell...

Then use something like this:

Code: Select all

<?php

// $cells is the array holding the cell contents

$numcells = count($cells);
$perline = 3;
$lines = $numcells / $perline;
if (!is_int($lines)) { $lines = intval($lines) + 1;}
$n = 0;

for ($i = 1; $i <= $lines; $i++) {
	$table .= '<tr>';
	for ($i2 = 1; $i2 <= $perline; $i2++) {
		$table .= '<td>'.$cells[$n].'</td>';
		$n++;
	}
	$table .= '</tr>';
}

$table = '<table>'.$table.'</table>';

?>
The beauty is, that if you want to change it to 4 columns at some point, all you have to change is the $perline variable.

Posted: Wed Oct 15, 2003 9:28 am
by loco_ola
Sorry, like im said pretty new to php, but how do i relate that to my information, and how do i populate the array?

Posted: Wed Oct 15, 2003 9:39 am
by choppsta
Some of this is guessing what your code is, but something like the following before the other code will get your table cells into an array...

Code: Select all

<?php
mysql_select_db($database_conwildtangent, $conwildtangent); 

$query_rsservicetext = "SELECT tblprojects.projectid, tblprojects.projectname, tblprojects.projecticon, tblprojects.projecttext, tblservicedata.servicedataid, tblprojectassociations.projectassociationid, tblprojectassociations.projectid, tblprojectassociations.servicetypeid FROM tblprojects INNER JOIN (tblprojectassociations INNER JOIN tblservicedata ON tblprojectassociations.servicetypeid=tblservicedata.servicedataid) ON tblprojectassociations.projectid=tblprojects.projectid WHERE tblservicedata.servicetypeid=5";

$query = mysql_query($query_rsservicetext);

if (mysql_num_rows($query) > 0) {
	while ($row_rsservicetext = mysql_fetch_array($query, MYSQL_ASSOC)) {
		$cells[] = '<h6><a href="projects.php?projectid='.$row_rsservicetext['projectid'].'<img src="images/'.$row_rsservicetext['projecticon'].'" alt="'.$row_rsservicetext['projecticon'].'" name="icon_3dvis" border="0" id="icon_3dvis"><strong><br></strong></a><a href="projects.php?projectid='. $row_rsservicetext['projectid'].'"><strong>'.$row_rsservicetext['projectname'].'</strong></a></h6>';
	}
}
?>

Posted: Wed Oct 15, 2003 9:40 am
by Nay
I'm making a guess, but it's most likely:

Code: Select all

$query_rsservicetext = "SELECT tblprojects.projectid, tblprojects.projectname, tblprojects.projecticon, tblprojects.projecttext, tblservicedata.servicedataid, tblprojectassociations.projectassociationid, tblprojectassociations.projectid, tblprojectassociations.servicetypeid FROM tblprojects INNER JOIN (tblprojectassociations INNER JOIN tblservicedata ON tblprojectassociations.servicetypeid=tblservicedata.servicedataid) ON tblprojectassociations.projectid=tblprojects.projectid WHERE tblservicedata.servicetypeid=5";

$r = mysql_query($query_rsservicetext, $link);
$cell = mysql_fetch_array($r);
Hope that helps,

-Nay

Looking promising but....

Posted: Wed Oct 15, 2003 9:49 am
by loco_ola
Thanks for the help guys i really appreciate it, im getting errors about

Undefined variable "table" Here's my code...

Code: Select all

<?php
<?php require_once('Connections/conwildtangent.php'); ?>

<?php 
mysql_select_db($database_conwildtangent, $conwildtangent); 

$query_rsservicetext = "SELECT tblprojects.projectid, tblprojects.projectname, tblprojects.projecticon, tblprojects.projecttext, tblservicedata.servicedataid, tblprojectassociations.projectassociationid, tblprojectassociations.projectid, tblprojectassociations.servicetypeid FROM tblprojects INNER JOIN (tblprojectassociations INNER JOIN tblservicedata ON tblprojectassociations.servicetypeid=tblservicedata.servicedataid) ON tblprojectassociations.projectid=tblprojects.projectid WHERE tblservicedata.servicetypeid=5"; 

$query = mysql_query($query_rsservicetext); 

if (mysql_num_rows($query) > 0) { 
   while ($row_rsservicetext = mysql_fetch_array($query, MYSQL_ASSOC)) { 
      $cells[] = '<h6><a href="projects.php?projectid='.$row_rsservicetext['projectid'].'<img src="images/'.$row_rsservicetext['projecticon'].'" alt="'.$row_rsservicetext['projecticon'].'" name="icon_3dvis" border="0" id="icon_3dvis"><strong><br></strong></a><a href="projects.php?projectid='. $row_rsservicetext['projectid'].'"><strong>'.$row_rsservicetext['projectname'].'</strong></a></h6>'; 
   } 
} 
?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php 

// $cells is the array holding the cell contents 

$numcells = count($cells); 
$perline = 3; 
$lines = $numcells / $perline; 
if (!is_int($lines)) { $lines = intval($lines) + 1;} 
$n = 0; 

for ($i = 1; $i <= $lines; $i++) { 
   $table .= '<tr>'; 
   for ($i2 = 1; $i2 <= $perline; $i2++) { 
      $table .= '<td>'.$cells[$n].'</td>'; 
      $n++; 
   } 
   $table .= '</tr>'; 
} 

$table = '<table>'.$table.'</table>'; 

?> 
</body>
</html>

?>
THANKS AGAIN!

Posted: Wed Oct 15, 2003 9:54 am
by Stoneguard
On your last line, I believe you need some breathing room:

Code: Select all

<?php
$table = '<table>'.$table.'</table>'; 
?>
would be

Code: Select all

<?php
$table = '<table>' .$table. '</table>'; 
?>

Posted: Wed Oct 15, 2003 9:56 am
by loco_ola
still has the Undefined variable: table - error

this occurs before that line in the code.

Posted: Wed Oct 15, 2003 9:59 am
by Stoneguard
How about posting the specific line it occurs on then?

Posted: Wed Oct 15, 2003 10:00 am
by choppsta
Can you post the actual error message?

Also, you need to echo out the table variable at the end.

Code: Select all

<?php
echo $table;
?>

Posted: Wed Oct 15, 2003 10:01 am
by Nay
Yeah, you'll get a undefined table if your error_reporting in the php.ini is set to E_ALL. If you can get access to the php.ini file then open it up and search for:
error_reporting
When you find that, change the value of it to
error_reporting = E_ALL & ~E_NOTICE
If you can't get access to the php.ini file then you can use:

Code: Select all

<?php
error_reporting  (E_ERROR | E_WARNING | E_PARSE);
?>
Hope that helps,

-Nay

Posted: Wed Oct 15, 2003 10:13 am
by loco_ola
Looking good but it doesnt put the image in for some reason?

Posted: Wed Oct 15, 2003 10:15 am
by loco_ola
Got it, was missing a >

MANY THANKS ALL OF YOU!!!

Posted: Wed Oct 15, 2003 10:22 am
by Nay
Well, glad it worked. I'm just wondering why would you want to exit PHP when the next code is also a PHP code (eg: after require_once()). Anyhow, I just refreshed to see if there was any new posts - seems there was. I re-wrote your script to be a little bit more "proper". Hope you can learn somethings from it:

Code: Select all

<?php

require_once("Connections/conwildtangent.php");

mysql_select_db($database_conwildtangent, $conwildtangent);

$query_rsservicetext = "SELECT tblprojects.projectid, tblprojects.projectname, tblprojects.projecticon, tblprojects.projecttext, tblservicedata.servicedataid, tblprojectassociations.projectassociationid, tblprojectassociations.projectid, tblprojectassociations.servicetypeid FROM tblprojects INNER JOIN (tblprojectassociations INNER JOIN tblservicedata ON tblprojectassociations.servicetypeid=tblservicedata.servicedataid) ON tblprojectassociations.projectid=tblprojects.projectid WHERE tblservicedata.servicetypeid=5";

$query = mysql_query($query_rsservicetext) or die(mysql_error());

if (mysql_num_rows($query) > 0) {

while($row_rsservicetext = mysql_fetch_array($query) {

$cells[] = <<< CELLS
<h6>
<a href="projects.php?projectid={$row_rsservicetext['projectid']}">
<img src="images/{$row_rsservicetext['projecticon']}" alt="{$row_rsservicetext['projecticon']}" name="icon_3dvis" border="0" id="icon_3dvis" /></a>
<br />
<a href="projects.php?projectid={$row_rsservicetext['projectid']}">
<strong>{$row_rsservicetext['projectname']}</strong></a>
</h6>
CELLS;

}

}

echo <<< END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
END;

$numcells = count($cells);
$perline = 3;
$lines = $numcells / $perline;
if (!is_int($lines)) { $lines = intval($lines) + 1;}
$n = 0;

for ($i = 1; $i <= $lines; $i++) {
   $table .= '<tr>';
   for ($i2 = 1; $i2 <= $perline; $i2++) {
      $table .= '<td>'.$cells[$n].'</td>';
      $n++;
   }
   $table .= '</tr>';
}

$table = "<table>" . $table . "</table>";

echo <<< EOF
</body>
</html>
EOF;

?>
-Nay

Posted: Wed Oct 15, 2003 10:28 am
by loco_ola
WOW!
thanks for the help (all) and the coding advice Nay, I didnt know you could do all that stuff with the code. <<< means until, i guess? very useful.

thanks again you saved me ages!