Here's what I got:
Code: Select all
$item = array('ham', 'egg', 'fish');
$itemLength = count($item);
for($x = 0; $x<$itemLength; $x++){
echo $item[$x];
}
thanks in advance!
Moderators: egami, macek, gesf
Code: Select all
$item = array('ham', 'egg', 'fish');
$itemLength = count($item);
for($x = 0; $x<$itemLength; $x++){
echo $item[$x];
}
Code: Select all
<?php
$item = array('ham', 'egg', 'fish');
$item[] = 'milk';
$item[4] = 'bread';
echo '<pre>';
print_r($item);
$itemLength = count($item);
for ($x = 0; $x < $itemLength; $x++) {
echo $item[$x] . '<br />';
}
Code: Select all
$Item=array("Palfy"=>1,"Calapso"=>2,"Moneray"=>3);
$sandwich=array('Palfy', 'calapso', 'moneray');
$ItemCount=count($sandwich);
for($i=1; $i < $ItemCount; $i++){
foreach($Item as $item=>$Iquant)
{
echo '<table border="2" cellspacing="0" cellpadding="5" width="900" height="100">
<tr>
<td rowspan="2" width="57" >'; echo $i . '</td>
<td width="552">'; echo $item . '</td>
<td width="147">'; echo 'Quantity: ' . $Iquant . '</td>
<td width="92" rowspan="2">'; echo 'Price: ' . $price . '</td>
</tr>
<tr>
<td height="20" colspan="2">'; echo $Details . '</td>
</tr>
</table>';
echo "<br>";
}}
Code: Select all
<?php
$sandwich=array('Palfy', 'calapso', 'moneray');
$ItemCount=count($sandwich);
echo '<table border="2" cellspacing="0" cellpadding="5" width="900" height="100">';
$i = 1;
foreach($Item as $item=>$Iquant)
{
echo '<tr>
<td rowspan="2" width="57" >'; echo $i . '</td>
<td width="552">'; echo $item . '</td>
<td width="147">'; echo 'Quantity: ' . $Iquant . '</td>
<td width="92" rowspan="2">'; echo 'Price: ' . $price . '</td>
</tr>
<tr>
<td height="20" colspan="2">'; echo $Details . '</td>
</tr>';
$i++;
}
echo '</table>';
Ok thanks! That works perfectly! now, how do I get the rows that I have already entered to stay displayed. For example if I leave the page and come back I'd like to see the same data there that I had just entered. same for if I wanted to enter additional new data. Would I have to use cookies or more sessions for this?seandisanti wrote:You're using some variables in there that aren't defined in the provided code, so I'm assuming they've got valid values. This should work.Code: Select all
<?php $sandwich=array('Palfy', 'calapso', 'moneray'); $ItemCount=count($sandwich); echo '<table border="2" cellspacing="0" cellpadding="5" width="900" height="100">'; $i = 1; foreach($Item as $item=>$Iquant) { echo '<tr> <td rowspan="2" width="57" >'; echo $i . '</td> <td width="552">'; echo $item . '</td> <td width="147">'; echo 'Quantity: ' . $Iquant . '</td> <td width="92" rowspan="2">'; echo 'Price: ' . $price . '</td> </tr> <tr> <td height="20" colspan="2">'; echo $Details . '</td> </tr>'; $i++; } echo '</table>';
Yah, it's just for a single visitor. How would I store a part of a array as a session variable? or would I have to store the entire array (from top of array values to the echo close table tag) as a new session variable (i think it begins with '$$', right?)? Either way I'd need some guidance.seandisanti wrote:It depends on how long you'd like to persist the data. If it's data you only need short term for a single visitor, then a session or cookie will do the job perfectly. If you're trying to create a persistent table that will be viewable by everyone, then a database is the way to go, and you can dynamically generate the table based on rows in the database; plus add or delete rows from the page. it's a bit more coding of course, but not very complex.
Code: Select all
<?php
switch( $_SESSION['Sandwich']){
case 'sandwich1':
$string1 = 'sandwich1';
array_push($new_array, $string1);
case 'Sandwich2':
$string2 = 'sandwich2';
array_push($new_array, $string2);}
$array = array();
$new_array = $array;
//I think I have to change this tho
for($i = 1; $i< $ItemCount;){
//then all the array organization and formatting down here
}
?>
Code: Select all
$_SESSION['sandwiches'] = array();
$blah = $_SESSION['sandwiches'];
public function saveToSession(){
$blah[]=$this;
}
Code: Select all
if (!empty ($blah)){
foreach ($blah as $sandwich){
$sandwich->displayFunctionThatDisplaysSandwichComponentsHoweverIWantThemToLook();
}
Yah, except $_SESSION[sandwiches'] does not fetch more than one variable at a time. It only goes through with one string (ex: 'Sandwich1') and carries it until the end where it outputs it in that table row.seandisanti wrote:I really think that an object oriented approach would be best. assuming you have a sandwich object, that has its own properties to store components etc; you could have a function that adds it to a session variable also like...and then returning a list of sandwiches would be as easy as:Code: Select all
$_SESSION['sandwiches'] = array(); $blah = $_SESSION['sandwiches']; public function saveToSession(){ $blah[]=$this; }
Code: Select all
if (!empty ($blah)){ foreach ($blah as $sandwich){ $sandwich->displayFunctionThatDisplaysSandwichComponentsHoweverIWantThemToLook(); }
except that my $_SESSION['sandwich'] does not contain a whole list of objects, im only able to assign a single object to it. you know what, now that im thinking about it i think my whole page structure is flawed. it may be better if I just go back and re-do them so that all the items are on one page. but i mean im just spitballing here. maybe a check box that returns the item name string to aseandisanti wrote:The $_SESSION['sandwiches'] would contain a whole list of objects. If you set a __toString() method in your sandwich class, then you could just explode $_SESSION['sandwiches'] with a delimiter string of '<br />' to have it print a whole list of all of them
Code: Select all
$_SESSON['sandwich']=toString($item1 + $item2 + ...+$item#)
So, I did get the cart to work with the String variable but it does not really function like a typical shopping cart. I found this tutorial for php shopping carts,http://jameshamilton.eu/content/simple- ... t-tutorial. It is well done but I'm having trouble fetching the variables from my URL after the form submits.seandisanti wrote:yes, $_SESSION['sandwiches'] should be an array of sandwich objects. I'm working right now, but i'll write up a sample sandwich class in a little bit if you want so you see what i mean
Code: Select all
<form action="/PRO/marketstreetdeli/jScript/cart/cart2.php?action=add&&id=1001" method="post"><input name="add" type="image" value="1001" src="/PRO/marketstreetdeli/images/Add.png" alt="submit button" /> The Erk </form>