Page 1 of 1

Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 1:21 pm
by liyun88
hi,i need to put number of items in shopping cart list..i dont know how to use foreach function..
can anyone help me to correct them??thanks in advance..

Code: Select all

 
 <td width="100px" bgcolor="#eeee76"><font color="#000000">NO</font></td>
<?php foreach ($_REQUEST['cartid'] as $k => $cartids){ ?>
                        <td width="100px"> <font color="#000000"><?php echo $k ;?></font></td>
<?php }?>

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 1:51 pm
by fugix
the foreach function only works for arrays, so you would have to store the user info into an array

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 1:54 pm
by liyun88
fugix wrote:the foreach function only works for arrays, so you would have to store the user info into an array
how to store the user info into array??can you guide me??thanks in advance..

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 2:33 pm
by fugix
use the array() function

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 10:28 pm
by liyun88
fugix wrote:use the array() function
i need to declare the array function??can you give me some example to me??not so familiar using array() function..
thanks in advance.

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 10:36 pm
by fugix

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 10:48 pm
by liyun88
isnt like this declare >$item = array($row->pid);
<?php foreach ($item as $k => $cartids){ ?>
<td width="100px"> <font color="#000000"><?php echo $k+1 ;?></font></td>
<?php }?>

but still get error Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\nagels\viewCart.php
can you help me to correct them??thanks in advance..

Re: Warning: Invalid argument supplied for foreach()

Posted: Sat Apr 16, 2011 11:15 pm
by fugix
do you want to display the keys or the values?

array($row->pid); would be array($row=>'pid');

<?php echo $k+1 ;?> would be <?php echo $k; ?>

Re: Warning: Invalid argument supplied for foreach()

Posted: Sun Apr 17, 2011 12:36 am
by liyun88
fugix wrote:do you want to display the keys or the values?

array($row->pid); would be array($row=>'pid');

<?php echo $k+1 ;?> would be <?php echo $k; ?>
i want to display like this no,item,price,quantity,total price
so i want display 1 item RM8.90 4 RM 40
2 item RM5 5 RM25

i just want to show the no of product in the cart..
but i dont know how to show this..

Code: Select all

<?php
			$query = "SELECT * FROM cart WHERE id=$id";
            $result = mysql_query($query);
		   
			
  
   if($row1= mysql_num_rows($result))
    { ?> 
                    <table border="1"bgcolor="#FFFFFF" cellpadding="0" cellspacing="0" width="500px">
                      <tr align="center"> 
                        <td width="100px" bgcolor="#eeee76"><font color="#000000">NO</font></td>
                        <td width="100px" bgcolor="#eeee76"><font color="#000000">Item Name</font></td>
                        <td width="100px" bgcolor="#eeee76"><font color="#000000">Unit Price(RM)</font></td>
                        <td width="100px" bgcolor="#eeee76"><font color="#000000">Quantity</font></td>
                        <td width="100px" bgcolor="#eeee76"><font color="#000000">Total Price(RM)</font></td>
                      </tr>
                      <?php
			
	while($row=mysql_fetch_object($result))
    { 
?>
                      
                      <tr>
					   $item = array($row=>'pid');
<?php foreach ($item as $k => $cartids){ ?>
<td width="100px"> <font color="#000000"><?php echo $k ;?></font></td>
<?php }?>
                        <td width="100px"> <font color="#000000"><?php echo $row->pname;?></font></td>
                        <td align=center width="100px"><font color="#000000"><nobr>RM 
                          <?php echo $row->uprice;?> </nobr></font></td>
                       
                            <table>
                              
but i failed to show the no of products..
can you help to correct them??
i try using your way,still have problem like before..
i dont know how to declare using array..
$row->pid is retrieve from row of product in database..