You'd simply use an if statement:
Code:$i = 0;
while($row=mysql_fetch_assoc($query)) {
//format the output
echo '<td><a href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'"></a><br />'.
'<a href="fullProduct.php?id='.$row['id'].'">'.$row['name'].'</a><br />£'.$row['price'].'</td>';
if(($i%4)==3) {
echo '</tr><tr>';
}
$i++;
}
Ive not seen that clause in an if statement before what exactly does it doo? It dont look quite right to me and an error is being thrown on line 56
Parse error: syntax error, unexpected T_ECHO in /home/ka751/public_html/WAT/products.php on line 56
Line 56 being
"echo '</tr><tr>';"
But something is telling me its with the clause in the IF statement
Actually it messed up.. weird
It's not suppose to be "if(($i%4)==3) {" but rather "if(($i%4)==3) {"
That looks like something more familiar
Would I not need to close the table tag at some point?
Ive now moved on somewhat and am trying to get some pagination going on. Its partially working in that it displays what page the user is currently on eg Page 1 of 2 but when I click next to naivgate to the next page the same results show but the variable $pagenum in the URL changes to 2
Below is my code thus far
What exaclty is going wrong?Code:<?php
$connection = mysql_connect ($host, $user, $passwd) or die ('Error connecting to mysql'); // connection to our mysql server
mysql_select_db ($dbName) or die ('Error connecting to selected database'); //connection to our selected database
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$query1 = mysql_query("SELECT * FROM tblItems") or die(mysql_error());
$rows = mysql_num_rows($query1);
//no results displayed per page
$page_rows = 4;
//pg no of last page
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
// sets range to display query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//display all products
$query2 = mysql_query("SELECT * FROM tblItems $max") or die(mysql_error());
echo '<table cellspacing="10"><tr>';
$i = 0;
while($row=mysql_fetch_assoc($query2)) {
echo '<td><a href="fullProduct.php?id='.$row['id'].'"><img src="image.php?id='.$row['id'].'"></a><br />'.
'<a href="fullProduct.php?id='.$row['id'].'">Name: '.$row['name'].'</a><br />Price: £'.$row['price'].'</td>';
if(($i%4)==3) {
echo '</tr><tr>';
}
$i++;
}
// shows the user what page they are on and the total number of pages
echo " Page $pagenum of $last<p>";
//check if we are on page one. if not page one display tags
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " <hr /> ";
// reverse above
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
I think I want to accomplish the same thing as your fullproduct.php.
http://northstarseries.com/tmp/phptest.php
I want the team name to link to the full team information. Is this similar?
Yea, pretty much the same concept. Use the ID in the query string to get the specific team's information.
Also, when you're passing an ID in the query string like that, and it's just a number, you can just cast it to an int. BlaineSch commented in his code to filter it, and I'm just following up on that. You can never over stress security.
Something like this will do just fine.
If you pass text though it, it will turn into a 0.Code:$id = (int)$_GET['id'];// FILTERED YAY!!
Do I have to have a different php file or do i run that query as phptest?id=1 ?
You have two separate files. One file will list all of the teams. The other file will give specific details for a team that you specify with an ID.
So I should write a teams.php that will extract out what I want. Makes sense. Thank you.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks