Jump to content

eval problem


lional

Recommended Posts

I am trying to create the expression:

$variable = $_POST['$variable'];

I am using the following code

<?php
            include 'includes/conn_db.php';
            $query_cl = "SELECT * from products";
$result_cl = mysql_query($query_cl, $conn);
while ($row_cl = mysql_fetch_assoc($result_cl)){

  $prod_id_out = $row_cl["prod_id"];
$description_out = $row_cl["description"];
$size_inch_out = $row_cl["size_inch"];
$size_mm_out = $row_cl["size_mm"];
$price_out = $row_cl["price"];
  $post_line = '$$prod_id_out = $_POST["$prod_id_out"];';
  eval("$$prod_id_out =  $_POST['$prod_id_out'];");
    
  
}

?>

I am getting the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\htdocs\cps\view_cart.php on line 83

What do I need to accomplish this - $variable = $_POST['$variable'];

My problem is that I am creating a dynamic product list where a client can purchase more than one product in a single transaction.

So I use my product id as the name in my select statement.

I therefore need my view cart to be able to evaluate the expression by using the eval function

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/
Share on other sites

It is not exactly clear from the posted code what you are doing with the information retrieved from the database. I am guessing you are outputting a form. If you post an example of what you are doing now, someone can provide direction on how to make it general purpose using arrays.

 

Edit: This thread might help as well - http://www.phpfreaks.com/forums/index.php/topic,214557.0.html

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-631920
Share on other sites

Here is an example of my code from my products list.

<?php
            include 'includes/conn_db.php';
            $query_cl = "SELECT * from products";
$result_cl = mysql_query($query_cl, $conn);
while ($row_cl = mysql_fetch_assoc($result_cl)){
  $prod_id_out = $row_cl["prod_id"];
$description_out = $row_cl["description"];
$size_inch_out = $row_cl["size_inch"];
$size_mm_out = $row_cl["size_mm"];
$price_out = $row_cl["price"];
      
      print <<<PRICES
            <td valign="top">
            <select name="$prod_id_out">
            <option value="0"></option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            </select></td>
            <td>
            $size_inch_out</td>
            <td>$size_mm_out</td>
            <td>$price_out</td>
            
            
            </tr>
PRICES;
}
?>

What I am trying to do is to list each product with a dropdown box for quantities giving options of 1 - 10. And a single order button at the bottom of the screen.

so if the customer wants 1 of product 1, 3 of product 4 then both of these products will just be added to the cart with the click of the order button.

My problem is that I need to dynamically assign a name for my select statement on the products page , and if the quantity is 1 or more for that particular product, then the product must be added to the shopping cart.

 

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-631928
Share on other sites

Change this line -

 

<select name="$prod_id_out">

 

to this -

 

<select name="product[$prod_id_out]">

 

and to iterate over the selected items -

 

<?php
foreach($_POST['product'] as $key => $value)
{
echo "Product id: $key, qty selected: $value<br />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-631993
Share on other sites

Thanks

This is the first cart I am writing that handles multiple products.

How will I retrieve the products on my add to cart page, so that I can view the products in my cart.

All my carts have an add to cart by each product

 

Thanks for all your assistance I really appreciate it

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-632068
Share on other sites

I have it working almost perfectly except if the page is refreshed I get the following error

Warning: Invalid argument supplied for foreach() in D:\htdocs\cps\view_cart.php on line 5

and the quantities and products form ends up blank

I am attaching my code:

foreach($_POST['product'] as $key => $value)

{

$qty1 = $value;

$pid = $key;

if (isset ($_SESSION['cart'][$pid])) {

$qty_out = $_SESSION['cart'][$pid] + $qty1;

} else {

$qty = $qty1;

}

// add to the cart session variable

$_SESSION['cart'][$pid] = $qty;

 

}

 

 

if (isset($_POST['submit'])) {

foreach ($_POST['qty'] as $key => $value) {

if (($value == 0) AND (is_numeric($value))) {

unset ($_SESSION['cart'][$key]);

} elseif (is_numeric($value) AND ($value > 0)) {

$_SESSION['cart'][$key] = $value;

}

}

}

// check if the shopping cart is empty

$empty = TRUE;

if (isset($_SESSION['cart'])) {

foreach($_SESSION['cart'] as $key => $value) {

if (isset($value)) {

$empty = FALSE;

 

}

}

}

// Display the cart if it is not empty

if (!$empty) {

    include 'includes/conn_db.php';

// Retrieve all of the information for the products in the cart

$query = 'SELECT * FROM products WHERE prod_id IN (';

foreach ($_SESSION['cart'] as $key => $value) {

$query .= $key . ',';

}

$query = substr ($query, 0, -1) . ') ORDER BY size_mm ASC';

$result = mysql_query($query);

 

// create a table and a form

print <<<TOP

<table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0">

          <tr><td height="450">

<table summary="" width="580" align="center" bgcolor="#b3314b">

<tr>

<td nowrap align="center"><font face="arial" size="2" color="white"><b>YOUR SHOPPING CART</b></font></tr></table>

<table summary="" align="center" width="580" border="1">

<tr>

<td>

<table border="0" width="580" cellpadding="3"bgcolor="#b3314b">

<tr>

<td align="left" width="250"><font face="arial" size="2" color="white"><b>Product</b></font></td>

<td align="center" width="70"><font face="arial" size="2" color="white"><b>Qty</b></font></td>

<td align="right" width="120"><font face="arial" size="2" color="white"><b>Unit Price</b></font></td>

<td align="right" width="120"><font face="arial" size="2" color="white"><b>Line Total</b></font></td>

</tr>

<form action="view_cart.php" method="post">

TOP;

// Print each item

$total = 0; // total cost of the order

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

/*

if ($row['specials_discount'] > 0) {

$list_price_out = $row['price'] / $row['specials_discount'];

} else {

      $list_price_out = $row['price'];

      }

      */

      $list_price_out = $row['price'];

      $list_price_out = number_format($list_price_out, 2, '.', '');

// Calculate the total and subtotals

$subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price_out;

$subtotal = number_format($subtotal, 2, '.', '');

$total += $subtotal;

$total = number_format($total, 2, '.', '');

if ($_SESSION['cart'][$row['prod_id']] > 0) {

// print the row

print <<<ROW

<tr>

 

<td align="left"><font face="arial" size="2" color="white">{$row['size_mm']}</font></td>

<td align="center">

           

            <input type="text" size="3" name="qty[{$row['prod_id']}]" value="{$_SESSION['cart'][$row['prod_id']]}"></td>

<td align="right"><font face="arial" size="2" color="white">R $list_price_out </font></td>

<td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td>

</tr>

ROW;

}

} // end of the WHILE loop

// print the footer and close the table and the form

print <<<FOOTER

<tr>

<td></td><td></td>

<td align="right" valign="top"><font face="arial" size="2" color="white"><b>Total:</b></font></td>

<td align="right" valign="top"><font face="arial" size="2" color="white"><b>R $total</b></font><br><br></td>

</tr>

</table>

  <table width="580" align="center">

<tr><td width="130" align="center"><input type="submit" name="submit" value="Update Cart"></form><form action="checkout.php" method="post"><input type="submit" name="checkout" value="Checkout"></form>

</td><td width="85"></td></tr></div>

FOOTER;

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-632550
Share on other sites

Part of your code is unconditionally executing anytime the page is requested. $_POST data is only present when the form submits to the page.

 

You need to put any code that access $_POST data inside if your existing code that is checking that $_POST['submit'] is set.

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-632677
Share on other sites

I need to do the quantity and if it is landscape or portrait. This is the code for my products page.

 

<?php
            include 'includes/conn_db.php';
            $query_cl = "SELECT * from products";
$result_cl = mysql_query($query_cl, $conn);
while ($row_cl = mysql_fetch_assoc($result_cl)){
  $prod_id_out = $row_cl["prod_id"];
$description_out = $row_cl["description"];
$size_inch_out = $row_cl["size_inch"];
$size_mm_out = $row_cl["size_mm"];
$price_out = $row_cl["price"];
      
      print <<<PRICES
      <tr>
            <td valign="top">
            <font face="arial" size="2" color="white"><input type="text" size="3" name="product[$prod_id_out]" value="0"></td>
            <td valign="top">
            <select name="or[$prod_id_out]">
            <option value="Landscape">Landscape</option>
            <option value="Portrait">Portrait</option>
            </select>
            </td>
            <td><font face="arial" size="2" color="white">
            $size_inch_out</td>
            <td><font face="arial" size="2" color="white">$size_mm_out</td>
            <td><font face="arial" size="2" color="white">$price_out</td>
            
            
            </tr>
PRICES;
}
?>[

 

I have created this page to add it to my cart but I think I have made a mistake

	if (isset($_POST['submit'])) {
		foreach ($_POST['product'] as $key => $value) {
			if (($value == 0) AND (is_numeric($value))) {
				unset ($_SESSION['cart'][$key]);
				} elseif (is_numeric($value) AND ($value > 0)) {
					$_SESSION['cart'][$key] = $value;
				}
			}
			foreach ($_POST['or'] as $key => $orient) {

					$_SESSION['cart'][$key] = $orient;
				}
			}

		// check if the shopping cart is empty
		$empty = TRUE;
		if (isset($_SESSION['cart'])) {
			foreach($_SESSION['cart'] as $key => $value) {
				if (isset($value)) {
					$empty = FALSE;

				}
			}
		}
	// Display the cart if it is not empty
	if (!$empty) {
    include 'includes/conn_db.php';
					// Retrieve all of the information for the products in the cart
		$query = 'SELECT * FROM products WHERE prod_id IN (';
		foreach ($_SESSION['cart'] as $key => $value) {
			$query .= $key . ',';
		}
		$query = substr ($query, 0, -1) . ') ORDER BY size_mm ASC';
		$result = mysql_query($query);

Link to comment
https://forums.phpfreaks.com/topic/122382-eval-problem/#findComment-638901
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.