Warning: Invalid argument supplied for foreach()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
liyun88
Forum Commoner
Posts: 51
Joined: Thu Mar 31, 2011 12:18 pm

Warning: Invalid argument supplied for foreach()

Post 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 }?>
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Warning: Invalid argument supplied for foreach()

Post by fugix »

the foreach function only works for arrays, so you would have to store the user info into an array
liyun88
Forum Commoner
Posts: 51
Joined: Thu Mar 31, 2011 12:18 pm

Re: Warning: Invalid argument supplied for foreach()

Post 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..
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Warning: Invalid argument supplied for foreach()

Post by fugix »

use the array() function
liyun88
Forum Commoner
Posts: 51
Joined: Thu Mar 31, 2011 12:18 pm

Re: Warning: Invalid argument supplied for foreach()

Post 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.
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Warning: Invalid argument supplied for foreach()

Post by fugix »

liyun88
Forum Commoner
Posts: 51
Joined: Thu Mar 31, 2011 12:18 pm

Re: Warning: Invalid argument supplied for foreach()

Post 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..
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: Warning: Invalid argument supplied for foreach()

Post 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; ?>
liyun88
Forum Commoner
Posts: 51
Joined: Thu Mar 31, 2011 12:18 pm

Re: Warning: Invalid argument supplied for foreach()

Post 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..
Post Reply