0

sorry if my question-title is confusing..i don't know exactly how to put it. but here's what:

so..im trying to get the data in my database, put it in

<select><option>

tag. and echo it.

addtocart.php this is my first code and this is working fine, with preset values

<?php
define('INCLUDE_CHECK',1);
require "../connectDB.php";

if(!$_POST['img']) die("There is no such product!");

$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));

echo json_encode(array(
    'status' => 1,
    'id' => $row['id'],
    'price' => (float)$row['price'],
    'txt' => '<table width="65%" id="table_'.$row['id'].'">
  <tr>
    <td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
    <td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
    <td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
    <td width="15%"><select name="qty[]" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');" style="width: 40px;">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option></select>

    </td>
    <td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
  </tr>
</table>'
));
?>

now my problem is that, like what i've said, i want to put the "quantity" numbers (which is in my database table) in the

<select><option>

..with that here's what i've done:

<?php
define('INCLUDE_CHECK',1);
require "../connectDB.php";

if(!$_POST['img']) die("There is no such product!");

$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));

echo json_encode(array(
    'status' => 1,
    'id' => $row['id'],
    'price' => (float)$row['price'],
    'txt' => '<table width="65%" id="table_'.$row['id'].'">
  <tr>
    <td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
    <td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
    <td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
    <td width="15%">
        $qty = $row["qty"];
        <select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">
        for ($i=0;$i<=$qty;$i++) {
        <option>' . $i . '</option>
        }
        </select>
    </td>
    <td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
  </tr>
</table>'
));
?>

my for-loop alone works well, but when i put that in echo json_encode, in addtocart.php the whole addtocart.php doesnt work.

i used the for-loop for select-option coz it is for quantity to be displayed in dropdown, like if whats in the database is 5 then it would appear as 1 2 3 4 5 in dropdown.

i don't know how to fix my code and i can't think of any other thing to replace the dropdown for selecting quantity of products/item with regards to my database data :( any suggestions? ..thanks in advance

silver01
  • 63
  • 2
  • 11
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 25 '16 at 15:13

1 Answers1

1

Your problem is that you are attempting to use a loop within string concatenation, which is not possible. You can get around that by using implode and range instead to generate the same string:

echo json_encode(array(
    'status' => 1,
    'id' => $row['id'],
    'price' => (float)$row['price'],
    'txt' => 
        '<table width="65%" id="table_'.$row['id'].'">
          <tr>
            <td width="5%" style="display:none;"><input type="text" name="id[]" value="'.$row['id'].'" style="width: 28px; border-width: 0px;"></td>
            <td width="35%"><input type="text" name="roomname[]" value="'.$row['name'].'" style="width: 95px; border-width: 0px;"></td>
            <td width="20%">Php <input type="text" name="price[]" value="'.$row['price'].'" style="width: 40px; border-width: 0px;"></td>
            <td width="15%">
                <select name="'.$row['id'].'_cnt" id="'.$row['id'].'_cnt" onchange="change('.$row['id'].');">
                <option>' . implode('</option><option>', range(0, $row["qty"])). '</option>
                </select>
            </td>
            <td width="15%"><a href="#" onclick="window.remove('.$row['id'].');return false;" class="remove">remove</a></td>
          </tr>
        </table>'
));

However the resulting code is still hard to read. If you are sending json data, it would make much more sense to just send the raw data and build the html client side with javascript.

Failing that, i would suggest you use output buffering to invert the operation, eg echoing php variables into html string, rather than the current method of concatenating strings around variables:

$img=mysql_real_escape_string(end(explode('/',$_POST['img'])));
$row=mysql_fetch_assoc(mysql_query("SELECT * FROM rooms_addons WHERE img='".$img."'"));
//start output buffer
ob_start();
//exit php mode
?>
    <table width="65%" id="table_<?=$row['id']?>">
        <tr>
            <td width="5%" style="display:none;">
                <input type="text" name="id[]" value="<?=$row['id']?>" style="width: 28px; border-width: 0px;"
            </td>
            <td width="35%">
                <input type="text" name="roomname[]" value="<?=$row['name']?>" style="width: 95px; border-width: 0px;">
            </td>
            <td width="20%">Php 
                <input type="text" name="price[]" value="<?=$row['price']?>" style="width: 40px; border-width: 0px;">
            </td>
            <td width="15%">
                <?php $qty = $row["qty"];?>
                <select name="<?=$row['id']?>_cnt" id="<?=$row['id']?>_cnt" onchange="change(<?=$row['id']?>);">
                    <?php for ($i=0;$i<=$qty;$i++) :?>
                    <option><?=$i?></option>
                    <?php endfor; ?>
                </select>
            </td>
            <td width="15%"><a href="#" onclick="window.remove(<?=$row['id']?>);return false;" class="remove">remove</a></td>
        </tr>
    </table>

<?php
//get html from output buffer

$html = ob_get_clean();
echo json_encode(array(
    'status' => 1,
    'id' => $row['id'],
    'price' => (float)$row['price'],
    'txt' => $html
));
?>

Finally mysql_* functions are depreciated, and removed from the current version of php - time to move to PDO instead

Steve
  • 20,703
  • 5
  • 41
  • 67
  • omg! it works! thanks so much man. output buffering never comes to my mind. jeez. and with PDO, i hope i can learn it easily, im used to using mysql_ hehe thanks ! – silver01 Jan 26 '16 at 06:18