Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

New row array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New row array

Postby sweetpeet » Wed Feb 20, 2013 2:45 pm

I am trying to create an array that will create a new row as new variables are entered into the array or one that saves the old row and adds the new one. I know in general how to create an array buy only for listing integers and strings. My problem is that I can't really organize the data.

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];
               }


I would like to be able to assign the $item[$x] to a variable and use that variable in other places throughout the page and still continue the list (not sure if thats very clear, let me know).

thanks in advance!
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Mon Feb 25, 2013 10:20 am

If i understand your question correctly; you just want to add a value to the next available index, right?
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 />';
}


seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Mon Feb 25, 2013 3:26 pm

Kind of... Yes I want the next available index but I want that index to show up with a table around each individual index.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby sweetpeet » Tue Feb 26, 2013 10:21 am

ok I just came up with something but I'm having a problem with the incremental number repeating duplicates and the item also repeating. any thoughts?

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>";
   }}
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Tue Feb 26, 2013 11:58 am

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>';
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Wed Feb 27, 2013 7:40 am

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>';
 


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?
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Wed Feb 27, 2013 8:55 am

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.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Wed Feb 27, 2013 9:14 am

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.


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.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Wed Feb 27, 2013 9:27 am

Personally I'm not a fan of variable variables ($$), even though they seem to be an accepted practice, in my opinion they hurt readability and obfuscate the purpose of your code. It's tough to offer a suggestion as to how to proceed without really knowing the context of the usage, but I would go with an OOP approach. Maybe you are adding component objects onto a sandwich object for purchase. You can use an autoload function to load classes as necessary, and then maybe you have $_SESSION['sandwiches'] contain an array of completed sandwich objects. It's also hard to make suggestions not knowing your individual comfort level with the language. You should check out jream.com's video tutorials, they can help out a lot. start at the bottom of the list at http://jream.com/learning/videos/php-oop if you'd like a quick crash course on OOP, or check out a 5 minute run through sessions at http://jream.com/learning/videos/php-ba ... e-security
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Thu Feb 28, 2013 10:30 am

Do you thing that using array_push() will work all the same?

this is basically what im thinking:

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

}
?>


In that above can i still put the incremental i++ at the end of the for statement?
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Thu Feb 28, 2013 1:14 pm

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...
Code: Select all

$_SESSION
['sandwiches'] = array();
$blah $_SESSION['sandwiches'];
 public function 
saveToSession(){
$blah[]=$this;
}
 

and then returning a list of sandwiches would be as easy as:
Code: Select all

if (!empty ($blah)){
foreach (
$blah as $sandwich){
$sandwich->displayFunctionThatDisplaysSandwichComponentsHoweverIWantThemToLook();
}
 
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Thu Feb 28, 2013 1:36 pm

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...
Code: Select all

$_SESSION
['sandwiches'] = array();
$blah = $_SESSION['sandwiches'];
 public function saveToSession(){
$blah[]=$this;
}

and then returning a list of sandwiches would be as easy as:
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.

Does array_push() not leave it in the array() after another is pushed in? I haven't tested my code yet i'm working on tailoring it and testing it right now.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Thu Feb 28, 2013 3:19 pm

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
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Thu Feb 28, 2013 4:53 pm

seandisanti 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


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 a
Code: Select all
$_SESSON['sandwich']=toString($item1 + $item2 + ...+$item#)


and i think I can put that session in an array_push($new_array, $_SESSION[sandwich']);

the way that ai have it set up now, the user has to go through the order process to cart and then click on the link for menu to add another item.


EDIT:
Oh, i think i see what your saying now. your saying that the $_SESSION['sandwich'] should pass more than one variable. i think im actually going that direction now.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm

Re: New row array

Postby seandisanti » Fri Mar 01, 2013 8:40 am

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
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: New row array

Postby sweetpeet » Mon Mar 04, 2013 8:42 am

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


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-php-shopping-cart-tutorial. It is well done but I'm having trouble fetching the variables from my URL after the form submits.

Heres my form code:
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>


and the PHP code that it submits to is the same as the tutorial.
sweetpeet
New php-forum User
New php-forum User
 
Posts: 22
Joined: Wed Feb 13, 2013 6:43 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron