How to use this array like table in database?

Hi, all! I want to take the first 5 records in array below, but I can’t. Can you help me? I don’t know name of this array, I’m only see it like table in database.


<?php
	$products = array(
		array('id' => '1', 'name' => 'broccoli', 'desc' => 'broccoli', 'price' => '1200'),
		array('id' => '2', 'name' => 'butter', 'desc' => 'butter', 'price' => '1300'),
		array('id' => '3', 'name' => 'cheese', 'desc' => 'cheese', 'price' => '1400'),
		array('id' => '4', 'name' => 'chickenleg', 'desc' => 'chicken leg', 'price' => '1500'),
		array('id' => '5', 'name' => 'chocolatecookies', 'desc' => 'chocolate cookies', 'price' => '1600'),
		array('id' => '6', 'name' => 'cornonthecob', 'desc' => 'corn on the cob', 'price' => '1700'),
		array('id' => '7', 'name' => 'freerangeeggs', 'desc' => 'free range eggs', 'price' => '1800'),
		array('id' => '8', 'name' => 'milk', 'desc' => 'milk', 'price' => '1900'),
		array('id' => '9', 'name' => 'organicmeatpatties', 'desc' => 'organic meat patties', 'price' => '2000'),
		array('id' => '10', 'name' => 'parmaham', 'desc' => 'parma ham', 'price' => '2010'),
		array('id' => '11', 'name' => 'pumpkinseedbun', 'desc' => 'pumpkin seed bun', 'price' => '2020'),
		array('id' => '12', 'name' => 'redcurrants', 'desc' => 'red currants', 'price' => '2030'),
		array('id' => '13', 'name' => 'sausages', 'desc' => 'sausages', 'price' => '2040'),
		array('id' => '14', 'name' => 'seedlesswatermelon', 'desc' => 'seedless watermelon', 'price' => '2050'),
		array('id' => '15', 'name' => 'sesameseedbagel', 'desc' => 'sesame seed bagel', 'price' => '2060'),
		array('id' => '16', 'name' => 'sunflowerseedloaf', 'desc' => 'sunflower seed loaf', 'price' => '2070')
	);
?>

You can access each nested array using the automatically set numbered keys (with each array being the value). So in this case you could use a “for” loop to iterate through the first five sub-arrays:


for($a=0;$a<=4;++$a)
{
	print_r($products[$a]);
}

#Outputs:


rray
(
    [id] => 1
    [name] => broccoli
    [desc] => broccoli
    [price] => 1200
)
Array
(
    [id] => 2
    [name] => butter
    [desc] => butter
    [price] => 1300
)
Array
(
    [id] => 3
    [name] => cheese
    [desc] => cheese
    [price] => 1400
)
Array
(
    [id] => 4
    [name] => chickenleg
    [desc] => chicken leg
    [price] => 1500
)
Array
(
    [id] => 5
    [name] => chocolatecookies
    [desc] => chocolate cookies
    [price] => 1600
)

I’m only see it like table in database.

Not sure I have read your request correctly, but it you only wanted the first 5 rows from the db in the first place, then you could have limited your db to only returning 5 with LIMIT.


$sql = "SELECT id
, name
, `desc`
, price 
FROM
mytable 
LIMIT 5";

** desc is a mysql protected word, hence it is quoted with backticks, presuming you ARE using mysql of course … If you are not using mysql then your RDBMS will have its own way of limiting the number of returned rows.

Thank a lot! But I want to work with database is array, not mysql or somethings. I want display data in array like this:

As you can see, I’m trying to paginate data. I calculated some cases and else display. I have a code but it not work:

for($x=$start; $x<=5; $x++){
echo ‘<tr>’;
echo ‘<td>’.$products[‘id’][$x].‘</td>’;
echo ‘<td><img src=img/’.$products[‘name’][$x].‘.png alt= />’.‘</td>’;
echo ‘<td>’.$products[‘desc’][$x].‘</td>’;
echo ‘<td>’.number_format($products[‘price’][$x], 2).‘</td>’;
echo ‘</tr>’;
}

Do you know what I said?

Can you show us how $products is created/defined? We understand you want to treat it as an array (modernW’s code did just that), however, since we don’t know how you are defining $products, it is impossible for us to tell you if the issue lies in looping through the records or with how you created $products.

My gut says, it is how your created $products, and that $products currently is NOT an array.

I also want to point out that StarLion’s suggestion is good too, and more likely you should be using LIMIT against your MySQL database (if that is what you are using), and using a second query to do the pagination by returning a COUNT of the products in the table.

Here is code index.php:


<?php include('data.dat'); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<link rel="stylesheet" type="text/css" href="css/paginate.css" />
    <title>Paginate</title>
</head>
<body>
	<?php
		//
		$start = (isset($_GET['start']) && (int)$_GET['start']) ? $_GET['start'] : 0;
		//
		$display = 5;
		// 
		$records = sizeof($products);
	?>
	<table border="1">
		<thead>
			<tr>
				<th>Id</th>
				<th>Image</th>
				<th>Name</th>
				<th>Price</th>
			</tr>
		</thead>
		<tbody>
		<?php //Display data is here
			for($x=$start; $x<=min($records, $start+$display); $x++){
				echo '<tr>';
				echo 	'<td>'.$products['id'][$x].'</td>';
				echo 	'<td><img src=img/'.$products['name'][$x].'.png alt= />'.'</td>';
				echo	'<td>'.$products['desc'].'</td>';
				echo 	'<td>'.number_format($products['price'][$x], 2).'</td>';
				echo '</tr>';
			}
		?>
		</tbody>
		<tfoot>
			<tr>
				<td>
					
					<?php
						// 
						if ($records > $display){
							// 
							$pages = ceil($records/$display);
						} else {
							$pages = 1;
						} 
						
						echo '<td colspan=4>';
							// 
							if($start!=0){
								echo '<a href=index.html.php?start='.($start-$display).'>Previous</a>';
							}
							// 
							if($pages > 1){
								echo '<div id=page><ul>';
								for($i=1; $i<=$pages; $i++){
									echo '<li><a href=index.html.php?start='.$display*($i-1).'>'.$i.'</a></li>';
								}
								echo '</ul></div>';
								// 
								if($start!= $display*($i-1)){
									echo '<a href=index.html.php?start='.($start+$display).'>Next</a>';
								}
							}
						echo '</td>';
					?>
				</td>
			</tr>
		</tfoot>
	</table>
</body>
</html>

And here is code data.dat:


<?php
	$products = array(
		array('id' => '1', 'name' => 'broccoli', 'desc' => 'broccoli', 'price' => '1200'),
		array('id' => '2', 'name' => 'butter', 'desc' => 'butter', 'price' => '1300'),
		array('id' => '3', 'name' => 'cheese', 'desc' => 'cheese', 'price' => '1400'),
		array('id' => '4', 'name' => 'chickenleg', 'desc' => 'chicken leg', 'price' => '1500'),
		array('id' => '5', 'name' => 'chocolatecookies', 'desc' => 'chocolate cookies', 'price' => '1600'),
		array('id' => '6', 'name' => 'cornonthecob', 'desc' => 'corn on the cob', 'price' => '1700'),
		array('id' => '7', 'name' => 'freerangeeggs', 'desc' => 'free range eggs', 'price' => '1800'),
		array('id' => '8', 'name' => 'milk', 'desc' => 'milk', 'price' => '1900'),
		array('id' => '9', 'name' => 'organicmeatpatties', 'desc' => 'organic meat patties', 'price' => '2000'),
		array('id' => '10', 'name' => 'parmaham', 'desc' => 'parma ham', 'price' => '2010'),
		array('id' => '11', 'name' => 'pumpkinseedbun', 'desc' => 'pumpkin seed bun', 'price' => '2020'),
		array('id' => '12', 'name' => 'redcurrants', 'desc' => 'red currants', 'price' => '2030'),
		array('id' => '13', 'name' => 'sausages', 'desc' => 'sausages', 'price' => '2040'),
		array('id' => '14', 'name' => 'seedlesswatermelon', 'desc' => 'seedless watermelon', 'price' => '2050'),
		array('id' => '15', 'name' => 'sesameseedbagel', 'desc' => 'sesame seed bagel', 'price' => '2060'),
		array('id' => '16', 'name' => 'sunflowerseedloaf', 'desc' => 'sunflower seed loaf', 'price' => '2070')
	);
?>

Thank for watching!

Something like this using array_chunk would be best fit I think:


    $products = array( 
        array('id' => '1', 'name' => 'broccoli', 'desc' => 'broccoli', 'price' => '1200'), 
        array('id' => '2', 'name' => 'butter', 'desc' => 'butter', 'price' => '1300'), 
        array('id' => '3', 'name' => 'cheese', 'desc' => 'cheese', 'price' => '1400'), 
        array('id' => '4', 'name' => 'chickenleg', 'desc' => 'chicken leg', 'price' => '1500'), 
        array('id' => '5', 'name' => 'chocolatecookies', 'desc' => 'chocolate cookies', 'price' => '1600'), 
        array('id' => '6', 'name' => 'cornonthecob', 'desc' => 'corn on the cob', 'price' => '1700'), 
        array('id' => '7', 'name' => 'freerangeeggs', 'desc' => 'free range eggs', 'price' => '1800'), 
        array('id' => '8', 'name' => 'milk', 'desc' => 'milk', 'price' => '1900'), 
        array('id' => '9', 'name' => 'organicmeatpatties', 'desc' => 'organic meat patties', 'price' => '2000'), 
        array('id' => '10', 'name' => 'parmaham', 'desc' => 'parma ham', 'price' => '2010'), 
        array('id' => '11', 'name' => 'pumpkinseedbun', 'desc' => 'pumpkin seed bun', 'price' => '2020'), 
        array('id' => '12', 'name' => 'redcurrants', 'desc' => 'red currants', 'price' => '2030'), 
        array('id' => '13', 'name' => 'sausages', 'desc' => 'sausages', 'price' => '2040'), 
        array('id' => '14', 'name' => 'seedlesswatermelon', 'desc' => 'seedless watermelon', 'price' => '2050'), 
        array('id' => '15', 'name' => 'sesameseedbagel', 'desc' => 'sesame seed bagel', 'price' => '2060'), 
        array('id' => '16', 'name' => 'sunflowerseedloaf', 'desc' => 'sunflower seed loaf', 'price' => '2070') 
    );
    echo "<pre>"; print_r(array_chunk($products, 5, true));

Somehow I missed this earlier, but your array dimensions are wrong

                echo '<tr>';
                echo     '<td>'.$products['id'][$x].'</td>';
                echo     '<td><img src=img/'.$products['name'][$x].'.png alt= />'.'</td>';
                echo    '<td>'.$products['desc'].'</td>';
                echo     '<td>'.number_format($products['price'][$x], 2).'</td>';
                echo '</tr>';

Should be

                echo '<tr>';
                echo     '<td>'.$products[$x]['id'].'</td>';
                echo     '<td><img src=img/'.$products[$x]['name'].'.png alt= />'.'</td>';
                echo    '<td>'.$products[$x]['desc'].'</td>';
                echo     '<td>'.number_format($products[$x]['price'], 2).'</td>';
                echo '</tr>';

$products = array( 
    array('id' => '1', 'name' => 'broccoli', 'desc' => 'broccoli', 'price' => '1200'), 
    array('id' => '2', 'name' => 'butter', 'desc' => 'butter', 'price' => '1300'), 
    array('id' => '3', 'name' => 'cheese', 'desc' => 'cheese', 'price' => '1400'), 
    array('id' => '4', 'name' => 'chickenleg', 'desc' => 'chicken leg', 'price' => '1500'), 
    array('id' => '5', 'name' => 'chocolatecookies', 'desc' => 'chocolate cookies', 'price' => '1600'), 
    array('id' => '6', 'name' => 'cornonthecob', 'desc' => 'corn on the cob', 'price' => '1700'), 
    array('id' => '7', 'name' => 'freerangeeggs', 'desc' => 'free range eggs', 'price' => '1800'), 
    array('id' => '8', 'name' => 'milk', 'desc' => 'milk', 'price' => '1900'), 
    array('id' => '9', 'name' => 'organicmeatpatties', 'desc' => 'organic meat patties', 'price' => '2000'), 
    array('id' => '10', 'name' => 'parmaham', 'desc' => 'parma ham', 'price' => '2010'), 
    array('id' => '11', 'name' => 'pumpkinseedbun', 'desc' => 'pumpkin seed bun', 'price' => '2020'), 
    array('id' => '12', 'name' => 'redcurrants', 'desc' => 'red currants', 'price' => '2030'), 
    array('id' => '13', 'name' => 'sausages', 'desc' => 'sausages', 'price' => '2040'), 
    array('id' => '14', 'name' => 'seedlesswatermelon', 'desc' => 'seedless watermelon', 'price' => '2050'), 
    array('id' => '15', 'name' => 'sesameseedbagel', 'desc' => 'sesame seed bagel', 'price' => '2060'), 
    array('id' => '16', 'name' => 'sunflowerseedloaf', 'desc' => 'sunflower seed loaf', 'price' => '2070') 
);
$chunks = array_chunk($products, 5, true);
$page = isset($_GET['page']) ? $_GET['page'] : 0;
if(isset($chunks[$page])){
    foreach($chunks[$page] as $product){
        echo $product['id'] . ' = ' . $product['name'] . '<br />';
    }
}

$x variable stand before column name is right. Can you say reason for it?

Thank!

You created a multi-dimensional array, the index of each array is numeric, since you didn’t specifically define a key, so it starts with 0 and goes to 15 (since you have 16 items).

$products[0] would store the data for broccoli, $products[1] contains the data for butter, etc.

Raju Gautam’s implementation of array_chunk is a good solution, it does require changing your code entirely to what he posted, but it does work well for this.

I got it. My code is working normally. Once again thank a lot.