Cant echo multiple values in array - php

I have something like this in my code:
<?php
foreach ($tomb2[1] as $key => $metaname){
$talalat = $tomb[1][$key];
echo '<p>' . "$metaname\n" . '</p>' . '<br>' . '<input type="text" name="metavalue[]" value="' . "$talalat\n" . '">' . '<br>';
}
?>
<input type="submit" name="Generálás" value="insert" onclick="insert()" />
</form>
I try to echo the several different values, however I get only the last one. Possibly the array contains only the last one. What am I doing wrong?

if you use a post method in form then you have to written $ertekek = $_POST["metavalue"] instead of $_GET["metavalue"] and then use print_r($ertekek)
instead of echo $ertekek;

You have written $talalat = $tomb[1][$key]; instead of $talalat = $tomb2[1][$key];

Related

How to append new elements to the end of a session in php?

I have a Session where I am storing some IDs and I would like to add to the end of that session a string (attach to its own respective id).
Here I have a table with some elements and I have some checkboxes there so when each checkbox is selected I am taking their ids and when posting I am storing those ids to a session:
<form method="post">
<?php if (!empty($arr_devices)) { ?>
<?php foreach ($arr_devices as $device) : ?>
<tr>
<td>
<input type="checkbox" name="devices[]" value="<?php echo $device["id"] ?>">
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["name"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["device_no"] . '</a>'; ?>
</td>
<td>
<?php echo '<a href=error_report.php?device_id=' . urlencode($device["id"]) . "&dev_name=" . urlencode($device["name"]) . "&imei=" . urlencode($device["serial_imei"]) . "&serial_no=" . urlencode($device["serial_no"]) . "&device_no=" . urlencode($device["device_no"]) . '>' . $device["barcode"] . '</a>'; ?>
</td>
<td>
<div class="input-group">
<textarea name="dev_comment" placeholder="Write your comment here" rows="1" cols="50"><?php echo $dev_comment; ?></textarea>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
<input class="btn" type="submit" name="submit" value="Report">
<form>
Here is the the way I am storing those IDs:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$_SESSION['devices_id'] = $_POST['devices'];
}
what I would like to do is to add the $dev_comment to that Session. So that when I use that Session I need to have the $dev_comments attached to each respective id. How can I implement that?
At first, ensure that $_SESSION['devices_id'] exists and is an array:
if(!isset($_SESSION['devices_id'])) {
$_SESSION['devices_id'] = [];
}
The, $_POST['devices'] is an array, you can easily merge:
$_SESSION['devices_id'] = array_merge($_SESSION['devices_id'], $_POST['devices']);

PHP insert array from multiple checked checkbox and text input into mySQL

I have an issue when trying to insert info coming from a form which is generated dynamically in php.
The form consists of of an variable amount of inputs which all consists of four input elements. See below how my form is generated:
<?php $result = mysql_query("SELECT id,name,description FROM todo_q WHERE todo_id = $todo_id AND active = 'y'");
while($todo_q=mysql_fetch_array($result)){
echo '<label>';
echo $todo_q['name'];
echo '</label><br>';
echo '<input type="checkbox" name="value[]" value="y" />';
//echo '<input type="hidden" name="value[]" value="n" />';
echo '<label>';
echo $todo_q['description'];
echo '</label><br>';
echo '<input type="text" id="comment" name="comment[]">';
echo '<input type="hidden" name="user_id[]" value="';
echo $user_id;
echo '" />';
echo '<input type="hidden" name="todo_id[]" value="';
echo $todo_q['id'];
echo '" />';
echo '<HR>';
}?>
And this is how I try to insert the info into mySQL:
$query = "INSERT INTO todo_a (value, comment, user_id, todo_id) VALUES ";
$query_parts = array();
for($x=0; $x<count($_POST["value"]); $x++){
$query_parts[] = "('" . $_POST['value'][$x] . "','" . $_POST['comment'][$x] . "'," . $_POST['user_id'][$x] . "," . $_POST['todo_id'][$x] . ")";
}
$q_parts = $query_parts;
foreach ($q_parts as $q_p){
$insert = ($query .= implode(',', $query_parts));
$result = mysql_query($insert);
}
The problem I have is that when check all checkboxes and comments everything is inserted on the right row in the DB, but if I skip to check one checkbox then it gets messed up...
I would like it the insert a new row if it the checkbox is checked and/or a comment is entered.
Can anybody point me in the right direction?
I tried to put a hidden input to get the value of unchecked checkboxes but i doesn't seem to work.. That why I have commented out the hidden checkbox.
PS. I know I should be using mysqli but this is an older site that I haven't upgraded yet..
You need to add index into input checkbox and comment name like :
$cbIndex = 0;
while($todo_q=mysql_fetch_array($result)){
echo '<label>';
echo $todo_q['name'];
echo '</label><br>';
// Generate checkbox with index of current result
echo '<input type="checkbox" name="value[' . $cbIndex . ']" value="y" />';
// Generate comment with index of current result
echo '<input type="text" id="comment" name="comment[' . $cbIndex . ']">';
echo '<input type="hidden" name="user_id[' . $cbIndex . ']" value="';
echo $user_id;
echo '" />';
echo '<input type="hidden" name="todo_id[' . $cbIndex . ']" value="';
echo $todo_q['id'];
echo '" />';
echo '<HR>';
// Inc of index
$cbIndex++;
}
When you submit your form, only checked checkbox will appear in $_POST["value"] :
foreach ($_POST["value"] as $cbIndex => $cbValue) {
$query_parts[] = "('" . $_POST['value'][$cbIndex] . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")";
// or
$query_parts[] = "('" . $cbValue . "','" . $_POST['comment'][$cbIndex] . "'," . $_POST['user_id'][$cbIndex] . "," . $_POST['todo_id'][$cbIndex] . ")";
}
...
Btw, you don't need to store value of checkbox, that will be 'y' all the time.
INFO That will be fine for a test app, but as commented by #Pogrindis and #John Conde, it's not safe code. MySQLi/PDO + prepare statement will avoid SQL injection.

Cant echo preg_match_all as it needed

I have an XML file with the following content:
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="ALALALAL"><vt:lpwstr>asdasda</vt:lpwstr></property><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="MACABSZ"><vt:lpwstr>ooooo</vt:lpwstr></property><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="3"><vt:lpwstr>c</vt:lpwstr></property><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="5" name="4"><vt:lpwstr>d</vt:lpwstr></property>
I have the following php. (I am able to fine desired parts of the xml):
if (preg_match_all ('_name="(.*?)"_', $ekker, $tomb2));
if (preg_match_all ('_<vt:lpwstr>(.*?)</vt:lpwstr>_', $ekker, $tomb));
In an html file I want to echo the results, so I use this:
<form>
<?php
foreach ($tomb2[1] as $metaname);
foreach ($tomb[1] as $talalat){
echo '<p>' . "$metaname\n" . '</p>' . '<br>' . '<input type="text" name="firstname" value="' . "$talalat\n" . '">' . '<br>';}?>
</form>
As a result I get this:
However this is not exactly what I want. Instead of the first three "4" I need: ALALALAL, MACABSZ, 3. Why does it echo only the last result of the 'name="(.*?)"' search?
Thanks for your answer!
From what I get, what you want is to access both arrays items with same index in one single loop. Maybe this will do :
<form>
<?php
foreach ($tomb2[1] as $key => $metaname){
$talalat = $tomb[1][$key];
echo '<p>' . "$metaname\n" . '</p>' . '<br>' . '<input type="text" name="firstname" value="' . "$talalat\n" . '">' . '<br>';
}
?>
</form>

How do I capture the value in a select list loop in PHP?

I have a gender array as an example, but I am also using longer select list such as states. I want to capture the option value in a PHP varible that the client side user chooses. Then I can use that captured value in an email message.
function genderList() {
$gender = array('M'=>"Male",'F'=>"Female",);
return $gender;
}
$gender = genderList();
$email_form = '<?php $states = statesList(); ?>
<form class="aw-contact-form" method="post" action="' . get_permalink() . '">
<label for="cf_gender">' . $label_gender . '</label>
<select name="gender" id="cf_gender">
<option selected="selected"></option>';
foreach ($gender as $key => $value) {
$email_form .= '<option value="' . $key . '">' . $value . '</option>';
}
$email_form .= '</select>
</form>
return $email_form;
Any help will be greatly appreciated.

PHP displays tags in different order

I have following problem, I have list of products in a database and want to display them in table unfortunately it plays some tricks for me because it displays one td before table even begins.
Here are the PHP functions:
<?php
function displayProduct($code,$url)
{
echo '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
echo '<input type="hidden" name="return_url" value="' . $url . '" />';
echo '<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
}
function displayItem($obj,$url)
{
echo '<tr>';
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>' . displayProduct($obj->code,$url) .'</td>';
echo '</tr>';
if(strlen($obj->description) > 2)
{
echo '<tr><td colspan="4" style="font-size: 10px;">' . $obj->description . '</td></tr>';
}
}
?>
And here is the HTML output that I get:
Could someone help me ?
The echo call from displayProduct happens before the echo call of displayItem occurs.
I can see two solutions.
1: displayProduct should return the things to write and not echo them.
2:
echo '<td>' . $obj->menuposition . '</td><td>' . $obj->name . '</td><td>' . '£'.$obj->price . '</td><td>';
displayProduct($obj->code,$url);
echo '</td>';
displayProduct($code,$url) should return the string instead of printing it out:
function displayProduct($code,$url)
{
$result = '<form method="post" action="cart_update.php"><input type="hidden" name="code" value="' . $code . '"/>';
$result .='<input type="hidden" name="return_url" value="' . $url . '" />';
$result .='<input type="hidden" name="type" value="add" /><input type="submit" value="Add" /></form>';
return $result
}
[Edit] I should read better the questions...
But this still applies:
Also as Adrian stated, you should not echo the lines in "displayProducts", but return a string.

Categories