PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 7:10 am
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
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ї'projectid']; ?>"><img src="images/<?php echo $row_rsservicetextї'projecticon']; ?>" alt="<?php echo $row_rsservicetextї'projecticon']; ?>" name="icon_3dvis" border="0" id="icon_3dvis"><strong><br>
</strong></a><a href="projects.php?projectid=<?php echo $row_rsservicetextї'projectid']; ?>"><strong><?php echo $row_rsservicetextї'projectname']; ?></strong></a></h6> </td>
Last edited by
loco_ola on Wed Oct 15, 2003 9:32 am, edited 1 time in total.
choppsta
Forum Contributor
Posts: 114 Joined: Thu Jul 03, 2003 11:11 am
Post
by choppsta » Wed Oct 15, 2003 9:25 am
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.
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 9:28 am
Sorry, like im said pretty new to php, but how do i relate that to my information, and how do i populate the array?
choppsta
Forum Contributor
Posts: 114 Joined: Thu Jul 03, 2003 11:11 am
Post
by choppsta » Wed Oct 15, 2003 9:39 am
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>';
}
}
?>
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Wed Oct 15, 2003 9:40 am
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
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 9:49 am
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!
Stoneguard
Forum Contributor
Posts: 101 Joined: Wed Aug 13, 2003 9:02 pm
Location: USA
Post
by Stoneguard » Wed Oct 15, 2003 9:54 am
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>';
?>
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 9:56 am
still has the Undefined variable: table - error
this occurs before that line in the code.
Stoneguard
Forum Contributor
Posts: 101 Joined: Wed Aug 13, 2003 9:02 pm
Location: USA
Post
by Stoneguard » Wed Oct 15, 2003 9:59 am
How about posting the specific line it occurs on then?
choppsta
Forum Contributor
Posts: 114 Joined: Thu Jul 03, 2003 11:11 am
Post
by choppsta » Wed Oct 15, 2003 10:00 am
Can you post the actual error message?
Also, you need to echo out the table variable at the end.
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Wed Oct 15, 2003 10:01 am
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
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 10:13 am
Looking good but it doesnt put the image in for some reason?
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 10:15 am
Got it, was missing a >
MANY THANKS ALL OF YOU!!!
Nay
Forum Regular
Posts: 951 Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia
Post
by Nay » Wed Oct 15, 2003 10:22 am
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
loco_ola
Forum Newbie
Posts: 15 Joined: Wed Oct 15, 2003 7:10 am
Post
by loco_ola » Wed Oct 15, 2003 10:28 am
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!