Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display 4 items per page from array w/ 150 items

Status
Not open for further replies.

frummel

Technical User
Jun 21, 2002
91
NL
Hi,

I have created an array, using the foreach() function. All well, the script lists a table for each item in the array. Unfortunately, I'd only like 4 items from the array displayed per page, using $_GET, so when going to URL: will display items 5-8 from the array.

Below the code I currently use:

Code:
<? 
$img_names = array (
	'102_1967',
	'102_1968',
	'102_1969',
	'102_1970',
	'102_1971',
	'102_1972',
	'102_1973',
	'102_1974',
	'102_1975',
	'102_1976',
	'102_1977',
	'102_1978',
	'102_1979',
	'102_1980',
	'102_1981',
	'102_1982',
	'102_1983',
	'102_1984',
	'102_1985',
	'102_1986',
	'102_1987',
	'102_1988',
	'102_1989',
	'102_1990',
	'102_1991',
	'102_1992',
	'102_1993',
	'102_1994',
	'102_1995',
	'102_1996',
	'102_1997',
	'102_1998',
	'102_1999',
	'102_2000',
	'102_2001',
	'102_2002',
	'102_2003',
	'102_2004',
	'102_2005',
	'102_2006',
	'102_2007',
	'102_2008',
	'102_2009',
	'102_2010',
	'102_2011',
	'102_2012',
	'102_2013',
	'102_2014',
	'102_2015',
	'102_2016',
	'102_2017',
	'102_2018',
	'102_2019',
	);
	
	foreach($img_names as $img_name) {
		echo "<table width='390' border='0' cellpadding='0' cellspacing='0'>";
		echo "<tr>";
    	echo "<td width='20' valign='top'><input type='checkbox' name='checkbox3' value='checkbox'></td>";
    	echo "<td width='120' valign='top' class='border'><a href='main.php?img_id=$img_name' target='_parent' name='$img_name'>";
		echo "<img src='img/" . $img_name . "_pvw.gif' width='118' height='89'></a></td>";
   	 	echo "<td width='10'>&nbsp;</td>";
    	echo "<td valign='top' class='img_description'><span class='style2'>Datum toegevoegd: $img_date<br>";
      	echo "Locatie: $img_city, $img_country</span></td>";
  		echo "</tr>";
  		echo "<tr>";
    	echo "<td width='20'>&nbsp;</td>";
    	echo "<td width='120'>&nbsp;</td>";
    	echo "<td width='10'>&nbsp;</td>";
    	echo "<td>&nbsp;</td>";
  		echo "</tr>";
		echo "</table>";
}
?>

Has anyone an idea for a good approach to achieve what I need?

Thanks!
 
Hi, I made this sample code for you:

Code:
<?php

$img_names = array (
    '102_1967',
    '102_1968',
    '102_1969',
    '102_1970',
    '102_1971',
    '102_1972',
    '102_1973',
    '102_1974',
    '102_1975',
    '102_1976',
    '102_1977',
    '102_1978',
    '102_1979',
    '102_1980',
    '102_1981',
    '102_1982',
    '102_1983',
    '102_1984',
    '102_1985',
    '102_1986',
    '102_1987',
    '102_1988',
    '102_1989',
    '102_1990',
    '102_1991',
    '102_1992',
    '102_1993',
    '102_1994',
    '102_1995',
    '102_1996',
    '102_1997',
    '102_1998',
    '102_1999',
    '102_2000',
    '102_2001',
    '102_2002',
    '102_2003',
    '102_2004',
    '102_2005',
    '102_2006',
    '102_2007',
    '102_2008',
    '102_2009',
    '102_2010',
    '102_2011',
    '102_2012',
    '102_2013',
    '102_2014',
    '102_2015',
    '102_2016',
    '102_2017',
    '102_2018',
    '102_2019',
    );

function getList($start = 0, $num = 4, $list) {
  if ($start + $num > count($list)) { // dont let it count past the list
      $max = count($list);
    }
  else {
      $max = $start + $num;
    }

  for ($i = $start; $i < $max; $i++) {
      $foo .= $list[$i] . "<br />";
    }
  return $foo;
}
echo getList(0, 4, $img_names);
?>

Olav Alexander Mjelde
Admin & Webmaster
 
Thanks DaButcher for your fast reply.
I edited your script to fill my needs, and what I have so far is working:

Code:
<?php

$img_names = array (
    '102_1967',
    '102_1968',
    '102_1969',
    '102_1970',
    '102_1971',
    '102_1972',
    '102_1973',
    '102_1974',
    '102_1975',
    '102_1976',
    '102_1977',
    '102_1978',
    '102_1979',
    '102_1980',
    '102_1981',
    '102_1982',
    '102_1983',
    '102_1984',
    '102_1985',
    '102_1986',
    '102_1987',
    '102_1988',
    '102_1989',
    '102_1990',
    '102_1991',
    '102_1992',
    '102_1993',
    '102_1994',
    '102_1995',
    '102_1996',
    '102_1997',
    '102_1998',
    '102_1999',
    '102_2000',
    '102_2001',
    '102_2002',
    '102_2003',
    '102_2004',
    '102_2005',
    '102_2006',
    '102_2007',
    '102_2008',
    '102_2009',
    '102_2010',
    '102_2011',
    '102_2012',
    '102_2013',
    '102_2014',
    '102_2015',
    '102_2016',
    '102_2017',
    '102_2018',
    '102_2019',
    );

	if (empty($_GET['page_no']) ) {
		$page_no = 00;
		$start_value = $page_no * 4;
		$num_value = 4;
		}
		else {
			$start_value = $_GET['page_no'] * 4;
			$num_value = 4;
			}

	function getList($start = '$start_value', $num = '$num_value', $list) {
 		 if ($start + $num > count($list)) { // dont let it count past the list
      		$max = count($list);
    		}
  		else {
      		$max = $start + $num;
    		}

  		for ($i = $start; $i < $max; $i++) {
      		$foo .= $list[$i] . "<br />";
    		}
  		return $foo;
	}

echo getList($start_value, $num_value, $img_names);
?>

However I get the error message below, on top of the correct results:
Code:
Notice: Undefined variable: foo in /var/[URL unfurl="true"]www/html/LL05/test_array.php[/URL] on line 78

I know I can turn to: error_reporting = Off, but I'd like to know if there is a way to get around this without having to do this.

Thanks again!
 
Change
Code:
    function getList($start = '$start_value', $num = '$num_value', $list) {
          if ($start + $num > count($list)) { // dont let it count past the list
              $max = count($list);
            }
          else {
              $max = $start + $num;
            }

          for ($i = $start; $i < $max; $i++) {
              $foo .= $list[$i] . "<br />";
            }
          return $foo;
    }
to
Code:
    function getList($start = '$start_value', $num = '$num_value', $list) {
          if ($start + $num > count($list)) { // dont let it count past the list
              $max = count($list);
            }
          else {
              $max = $start + $num;
            }
          foo = '';
          for ($i = $start; $i < $max; $i++) {
              $foo .= $list[$i] . "<br />";
            }
          return $foo;
    }
 
Thanks MattNeeley for your reply too.
I was guessing it had to be like you wrote, but maybe I should've just done instead of asking...

Well here's the next issue, well not necessarily an issue, but again a thing I can't seem to get past...

Below the scripting I have done so far:

-> test_array.php
Code:
<?php

	$img_id = $_GET['img_id'];
	$page_next = $_GET['page_no'] + 1;
	
	echo "<form name='ptest' method='post' action='main.php?page_no=$page_next&img_id=$img_id'>";

	$img_names = array (
		'1967',
		'1968',
		'1969',
		'1970',
		'1971',
		'1972',
		'1973',
		'1974',
		'1975',
		'1976',
		'1977',
		'1978',
		'1979',
		'1980',
		'1981',
		'1982',
		'1983',
		'1984',
		'1985',
		'1986',
		'1987',
    );
		
	$start_value = $_GET['page_no'] * 4;
	$num_value = 4;
			
	function getList($start = '$start_value', $num = '$num_value', $list) {
 		 if ($start + $num > count($list)) { // dont let it count past the list
      		$max = count($list);
    		}
  		else {
      		$max = $start + $num;
    		}
		$foo = '';
  		for ($i = $start; $i < $max; $i++) {
			$page_id = $_GET['page_no'];
			$img_date = '22-08-2005';
			$img_city = 'Biddinghuizen';
			$img_country = 'NL';
			$foo .= "<table width='390' border='0' cellpadding='0' cellspacing='0'>
			<tr>
			<td width='20' valign='top'><input type='checkbox' name='" . $list[$i] ."' value='" . $list[$i] . "'></td>
			<td width='120' valign='top' class='border'><a href='main.php?page_no=$page_id&img_id=$list[$i]' name='$list[$i]'>
			<img src='img/102_" . $list[$i] . "_pvw.gif' width='118' height='89'></a></td>
			<td width='10'>&nbsp;</td>
			<td valign='top' class='img_description'><span class='style2'>Datum toegevoegd: $img_date<br>
			Locatie: $img_city, $img_country</span></td>
			</tr>
			<tr>
    		<td width='20'>&nbsp;</td>
    		<td width='120'>&nbsp;</td>
    		<td width='10'>&nbsp;</td>
    		<td>&nbsp;</td>
  			</tr>
			</table>"; 
    		}
  		return $foo;

	}

	echo getList($start_value, $num_value, $img_names);

	echo "<table width='390' border='0' cellpadding='0' cellspacing='0'><td align='right'><input type='submit' name='Submit' value='Next page'></td></table>";
	echo "</form>";
	
?>

-> main.php
Code:
<html>
<head>
<title>LL'05 Foto's</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="main.css" rel="stylesheet" type="text/css">
</head>

<body bgcolor="#F4F4F4">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div align="right"><img src="ll05_logo_small.gif" width="99" height="35"></div></td>
  </tr>
  <tr>
    <td><span class="style2">Foto's <strong>&gt;</strong> <em>Lowlands 2005 (19-08-2005 tm 22-08-2005)</em></span></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td height="40" colspan="3"><h1>Lowlands 2005 (19-08-2005 tm 22-08-2005)</h1></td>
  </tr>
  <tr>
    <td width="300" height="225" valign="top" class="border"><img src="img/102_<? echo $_GET['img_id']; ?>_pvw.jpg" width="298" height="225"></td>
    <td width="40" rowspan="4" valign="top" background="bg_dot.gif">&nbsp;</td>
    <td rowspan="4" valign="top"><? require('test_array.php'); ?></td>
  </tr>
  <tr>
    <td width="300" height="20" valign="top" class="border">&nbsp;</td>
  </tr>
  <tr>
    <td width="300" height="150" valign="top" class="border"><p>&nbsp;</p>    </td>
  </tr>
  <tr>
    <td width="300" height="20" valign="bottom">&nbsp;</td>
  </tr>
</table>
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="668" height="30">&nbsp;</td>
    <td width="10">&nbsp;</td>
    <td width="72"><div align="right">
    </div></td>
  </tr>
  <tr valign="bottom">
    <td height="30" colspan="3"><div align="center">&copy;2005</div></td>
  </tr>
</table>
</body>
</html>

To see the script as it is currently working, you might understand what exactly is it I'd like to accomplish.
Check for the script if you like.

As you may notice, every image that is displayed has a checkbox next to it. A visitor must be able to check a checkbox if he/she wants to download this particular image, or more than one. The visitor then clicks the button in the right, and gets to the next 4 images from the array, can check checkboxes again and click the button again, and on, and on... In the end, the user clicks a Download button, and all selected images are packed in 1 zipfile which is created on the fly and then downloaded.

I want to use sessions, in which all checked checkboxes are maintained until the page with the download button appears. The problem I ran into is: I need to use some kind of variable inside 'function getList' which keeps changing the values of 'session_register()' function, which is in some way parallell with the 4 images displayed at that time.

My apologies for my perhaps poor English, I'm only Dutch :p

If anyone has any ideas, it would again be great!

Thanks again!
 
Why don't you just store an array in the session data with the exact file names of every file checked? Then, at the end when they hit download you can just do a foreach and run quickly through the array to pack all the images. Two birds with one stone.
 
yeah, you dont need to do this in the function.
when the user clicks an image and submits the form, set the session variable to the array of checkboxes.

ps. remember to name the checkboxes so you get them in an array :)

good luck!
I'm off to lunch

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top