Jump to content

Recommended Posts

Having a few issues with this code, it was used on another site of mine, but haven't dabbled much with the foreach syntax, I know this doesn't work, it used to pull the info from a flat file, I need this one to work from a database.

 

All I'm trying to achieve is to pull all the details from the database, spit out each row, then based on how many entries are in the database, paginate 6 per page (two per line, three lines down, the next page).  I suspect that works ok, but with so many different variables, I'm very confused.  I know for a fact the foreach is wrong, just not sure what I need to do with that and if it affects the pagination results.

 

Here's the code:

 

<?php

		$start = (isset($_GET['page'])) ? ($_GET['page'] - 1) * 6 : 0;
		$page = ($start == 0) ? 1 : $_GET['page'];
		$result = mysql_query("SELECT * FROM items") or die(mysql_error()); 
		$i=-1;
		foreach ($row) {
			$i++;
			if ($start > $i) { continue; }
			if (($i - $start) > 5) { break; }
			if (($i % 2) == 0) { echo '<tr>'; }
			while($row = mysql_fetch_array( $result )) {		
			?>
			<td><table border="1" cellpadding="0" cellspacing="0" bordercolor="#333333">
			  <tr>
				<td><?php echo $row['title']; ?></td>
			  </tr>
			</table></td>
			<?php
			}
			if (($i % 2) == 1) { echo '</tr>'; }
		}
		if (($i % 2) != 1) { echo '</tr>'; }
		?>
            </table>Page <?php 
		for ($i = 1;$i <= count($result);$i = $i + 6) {
			$tmp = (($i + 5) / 6);
			echo "<a href='cat.php?page=$tmp'>$tmp</a> ";
		}
		if ($tmp != $page) { ?><a href='cat.php?page=<?=($page + 1)?>'>Next >>></a><?php }
            
?>

 

I also suspect I have the while in the wrong location, but based on what the flat version pulled, this is roughly where I think it should fit.

 

Thanks!

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/
Share on other sites

try this out

<table>
  <tr>
<?php
  $start = (isset ($_GET['page'])) ? ($_GET['page'] - 1) * 6 : 0;
  $page = ($start == 0) ? 1 : $_GET['page'];
  $result = mysql_query("SELECT * FROM items") or die(mysql_error());
  for($i = 0;$row = mysql_fetch_array($result);$i++){
    if ($start > $i) continue;
    if ($i - $start > 5) break;
    if (($i % 2) == 0) echo '</tr><tr>';
?>
  <td><table border="1" cellpadding="0" cellspacing="0" bordercolor="#333333">
    <tr>
     <td><?php echo $row['title']; ?></td>
    </tr>
  </table></td>
  <?php
  }
  echo "</table> Page";
  for ($i = 1; $i <= mysql_num_rows($result); $i += 6) {
    $tmp = ($i+5) / 6;
    echo "<a href='cat.php?page=$tmp'>$tmp</a> ";
  }
  if ($tmp != $page) {
    echo "<a href='cat.php?page=".($page + 1)."'>Next >>></a>";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-818202
Share on other sites

I think that the issue is that the $row in the foreach () is undefined.

 

This should be of the like:

<?php

         $start = (isset($_GET['page'])) ? ($_GET['page'] - 1) * 6 : 0;
         $page = ($start == 0) ? 1 : $_GET['page'];
         $result = mysql_query("SELECT * FROM items") or die(mysql_error()); 
         $i=-1;
         foreach (mysql_fetch_array( $result ) as $row) {...}
?>

 

but then you will need to do different stuff in the internal while loop. Once you get intot he foreach iteration, the rest is yours to decipher.

 

Another example of the syntax for the foreach is:

<?php
$string="string";
$array = str_split($string);
foreach($array as $char) print($char."<br/>");
?>

 

which will output:

s

t

r

i

n

g

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-818203
Share on other sites

Thanks guys, I follow how this works now, done what was suggested and it works perfectly.

 

Just another thing ....

 

I want to be able to pick out certain category id's (1 or 2 etc), but I want to be able to select all.  I've used $_GET to recover the catid from the url, but I can only select 1, 2, 3 etc, but how would I select ALL?  I've used the WHERE clause in the MySQL query, just not sure how to select all .....

 

Thanks again,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-818504
Share on other sites

have no clue what yer saying

 

Grab all records, dun add a WHERE clause

SELECT * FROM table

 

select 1 record, use a WHERE clause

SELECT * FROM table where x=1

 

if u have more than 1 record, use OR to seperate or in

SELECT * FROM table where x=1 OR x=2 OR x=3

SELECT * FROM table where x IN (1,2,3)

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-818515
Share on other sites

have no clue what yer saying

 

Grab all records, dun add a WHERE clause

SELECT * FROM table

 

select 1 record, use a WHERE clause

SELECT * FROM table where x=1

 

if u have more than 1 record, use OR to seperate or in

SELECT * FROM table where x=1 OR x=2 OR x=3

SELECT * FROM table where x IN (1,2,3)

 

Sorry, this is roughly want a I want to achive

 

cat.php?catid=1 = cameras

cat.php?catid=2 = lights

 

cat.php? = doesn't show anything ... I want anything with no entry to show all the rows in the table

 

Hope that helps,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-819145
Share on other sites

something like

<?php

$id=(isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'])>0))?int($_GET['id'])):0;

if($id)
  $where= " WHERE cat=$id";
$query="SELECT recs FROM table".$where;

 

the big if line does some validation, is it empty, does it have a number, and does it have an absolute value (positive).

 

simple validation like above, can save yer database from injections :)

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-819179
Share on other sites

something like

<?php

$id=(isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'])>0))?int($_GET['id'])):0;

if($id)
  $where= " WHERE cat=$id";
$query="SELECT recs FROM table".$where;

 

the big if line does some validation, is it empty, does it have a number, and does it have an absolute value (positive).

 

simple validation like above, can save yer database from injections :)

 

Thanks, I keep overlooking the injection issues, I'm still getting fully up to speed with PHP/MySQL.

 

I've used your code although had to remove two ')'s as it was causing an error, but now I've removed it, I get an error saying Fatal error: Call to undefined function int() in /home/stea7024/public_html/anthony/cat.php on line 88

 

Here's the code I have now:

 

$id = (isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'])>0)?int($_GET['id']):0;
  if($id)
  {
  $where= " WHERE cat=$id";
  $result = mysql_query("SELECT * FROM items".$where) or die(mysql_error());;
  }
  $start = (isset ($_GET['page'])) ? ($_GET['page'] - 1) * 4 : 0;
  $page = ($start == 0) ? 1 : $_GET['page'];
  // $result = mysql_query("SELECT * FROM items WHERE catid='$id'") or die(mysql_error());

 

the commented out line is the original query with the $id in .... I'm not too sure if i've done the wrong thing thing within the if($id).

 

Thanks,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-819581
Share on other sites

<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id']) && ($_GET['id'])>0)?0+$_GET['id']:0;
  if($id)
  {
     $where= " WHERE cat=$id";
     $result = mysql_query("SELECT * FROM items".$where) or die(mysql_error());;
  }
  $start = (isset ($_GET['page'])) ? ($_GET['page'] - 1) * 4 : 0;
  $page = ($start == 0) ? 1 : $_GET['page'];
  // $result = mysql_query("SELECT * FROM items WHERE catid='$id'") or die(mysql_error());

 

 

weird that int() doesnt work, if it dusn work, use 0+ for conversion :)

Link to comment
https://forums.phpfreaks.com/topic/155494-foreach-issue/#findComment-819614
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.