I am trying to add user input to a previously created array with array_merge. However, I am having trouble echoing the entire, new array as an unordered list. the user's entry is being processed correctly, but the original array is displaying as "Array" within the unordered list. Here is the code:
<?php
$travel = array("Automobile", "Jet", "Ferry", "Subway");
foreach ($travel as $t)
{
echo "<ul>";
echo "<li>$t</li>";
echo "</ul>";
}
?>
<form action="arrays.php" method="post">
<input type="text" name="added" />
<?php
foreach ($travel as $t)
{
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />";
}
?>
<input type="submit" name="submit" value="Add More!" />
</form>
<?php
$travel = array($_POST["travel"]);
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
Try using a new variable name for the new array created by array_merge(). I think you may run into problems modifying the array you're storing into.
$travel = array($_POST["travel"]);
should be
$travel = $_POST['travel'];
The problem was solved thus:
if (isset($_POST["submit"]))
{
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
}
?>
Related
I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>
So I'm wanting to run a function EVERY TIME a user clicks the btnChangeCat button...Im having trouble and I'm really not sure how to get it to work!
Im trying to run showCategory($_POST['chkCat'], 1)[0];and pass it the array of checked checkboxes when the user clicks the btnChangeCat button. Im not sure why this isn't working??
I've literally searched everywhere for a WORKING solution but can't find one that will work when passing the values to a function!
$showVideos = showCategory($categories, 1)[0];
$rowLength = showCategory($categories, 1)[3];
$likes = showCategory($categories, 1)[1];
$dislikes = showCategory($categories, 1)[2];
?>
<main>
<div class='controller-wrap'>
<div class='content-wrap'>
<h3>Category: </h3>hp
<form action='most-liked.php' method='post'>
<?php
$allCategories = ["All", "Sport", "Automotive", "Comedy", "Misc", "Surfing"];
$htmlOutput = "";
$i=0;
for($i;$i<sizeof($allCategories);$i++)
{
if($allCategories[$i] == "Surfing"){
$htmlOutput .= "<input selected='selected' type='checkbox' name='chkCat[]' value='".$allCategories[$i]."'/>";
}
else{
$htmlOutput .= "<input type='checkbox' name='chkCat[]' value='".$allCategories[$i]."'/>";
}
}
echo $htmlOutput;
?>
</form>
<br />
if(isset($_POST['chkCat'])){
if(is_array($_POST['chkCat'])) {
foreach($_POST['chkCat'] as $value){
echo $value;
}
}else{
$value = $_POST['chkCat'];
echo $value;
}
// showCategory($_POST['chkCat'], 1)[0];
}
isset($_POST['btnChangeCat'])){
// //How can I run this function with the array of CHECKBOXES??
showCategory($_POST['chkCat'], 1)[0];
}
?>
<input type='submit' value='CHANGE CATEGORIES' class='btnlike btnLike' name='btnChangeCat'/>
</div>
</main>
I get two warnings Warning: Invalid argument supplied for foreach() but I still retrieve my expected post variables. Where am I going wrong? It says the warning is in regard to line 7, which in this case is the line that begins; foreach($value as $k => $v)
<!------------- quote.php ----------------->
<body>
What services are you interested in? <br/><br/>
<form name="input" action="quote2.php" method="post">
<?php
$services = array('Tree Felling', 'Height Reduction', 'Crown Thinning', 'Deadwooding/Ivy Removal', 'Stump Grinding', 'Other');
foreach ($services as $option) { ?>
<input id="<?= $option ?>" type="checkbox" name="services[]" value="<?= $option ?>" />
<label for="<?= $option ?>"><?= $option ?></label>
<br />
<? }
?>
<br/>
<input name="name" type="text" />NAME</br>
<input name="place" type="text"/>TOWN</br/>
<input type="submit" value="Submit">
</form>
</body>
<!------------ quote2.php -------------->
<?php
echo '<h3>SERVICES REQUIRED</h3>';
foreach ($_POST as $key => $value) {
foreach($value as $k => $v)
{
echo '<p>'.$v.'</p>';
}
}
echo "<hr /><h3>DETAILS</h3>";
echo $name = $_POST['name'];
echo "</br>";
echo $place = $_POST['place'];
echo "<hr/>"
?>
A number of the form controls do not have names that end in [] and so are not arrays.
You can't loop over a string.
You should pull out each value of the submitted data individually and only loop over services.
The problem is that there is a very real possibility that not all of the items in your $_POST array will have values of type array; which you are assuming according to the code in quote2.php
A simple is_array() check will ensure that only arrays get iterated over in the foreach, here is the edited file contents:
<!------------ quote2.php -------------->
<?php
echo '<h3>SERVICES REQUIRED</h3>';
foreach ($_POST as $key => $value) {
if (is_array($value)) {
foreach($value as $k => $v)
{
echo '<p>'.$v.'</p>';
}
}
}
echo "<hr /><h3>DETAILS</h3>";
echo $name = $_POST['name'];
echo "</br>";
echo $place = $_POST['place'];
echo "<hr/>"
?>
That should do the trick.
Try wrapping your foreach loop in an if statement to check the array is an array. For example:
if (is_array($services)) {
foreach ($services as $option) {
// Some code
}
}
Although I wouldn't worry too much. This is just a cleaner way of writing your code.
I have following table.
<form method="post" action="test.php">
<input name="id[]" type="text" value="ID1" />
<input name="value[]" type="text" value="Value1" />
<hr />
<input name="id[]" type="text" value="ID2" />
<input name="value[]" type="text" value="Value2" />
<hr />
<input name="id[]" type="text" value="ID3" />
<input name="value[]" type="text" value="Value3" />
<hr />
<input name="id[]" type="text" value="ID4" />
<input name="value[]" type="text" value="Value4" />
<hr />
<input type="submit" />
</form>
And test.php file
<?php
$myarray = array( $_POST);
foreach ($myarray as $key => $value)
{
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
?>
But it is only returning this: <p>0</p><p>Array</p><hr />
What I'm doing wrong?
The foreach loops work just fine, but you can also simply
print_r($_POST);
Or for pretty printing in a browser:
echo "<pre>";
print_r($_POST);
echo "</pre>";
<?php
foreach ($_POST as $key => $value) {
echo '<p>'.$key.'</p>';
foreach($value as $k => $v)
{
echo '<p>'.$k.'</p>';
echo '<p>'.$v.'</p>';
echo '<hr />';
}
}
?>
this will work, your first solution is trying to print array, because your value is an array.
$_POST is already an array, so you don't need to wrap array() around it.
Try this instead:
<?php
for ($i=0;$i<count($_POST['id']);$i++) {
echo "<p>".$_POST['id'][$i]."</p>";
echo "<p>".$_POST['value'][$i]."</p>";
echo "<hr />";
}
?>
NOTE: This works because your id and value arrays are symmetrical. If they had different numbers of elements then you'd need to take a different approach.
You are adding the $_POST array as the first element to $myarray. If you wish to reference it, just do:
$myarray = $_POST;
However, this is probably not necessary, as you can just call it via $_POST in your script.
Why are you wrapping the $_POST array in an array?
You can access your "id" and "value" arrays using the following
// assuming the appropriate isset() checks for $_POST['id'] and $_POST['value']
$ids = $_POST['id'];
$values = $_POST['value'];
foreach ($ids as $idx => $id) {
// ...
}
foreach ($values as $idx => $value) {
// ...
}
Just:
foreach ( $_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
Because you have nested arrays, then I actually recommend a recursive approach:
function recurse_into_array( $in, $tabs = "" )
{
foreach( $in as $key => $item )
{
echo $tabs . $key . ' => ';
if( is_array( $item ) )
{
recurse_into_array( $item, $tabs . "\t" );
}
else
{
echo $tabs . "\t" . $key;
}
}
}
recurse_into_array( $_POST );
Came across this 'implode' recently.
May be useful to output arrays. http://in2.php.net/implode
echo 'Variables: ' . implode( ', ', $_POST);
$_POST is an array in itsself you don't need to make an array out of it. What you did is nest the $_POST array inside a new array. This is why you print Array.
Change it to:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
$_POST is already an array. Try this:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
As you need to see the result for testing purpose. The simple and elegant solution is the below code.
echo "<pre>";
print_r($_POST);
echo "</pre>";
I am trying to merge two arrays with array_merge(), but I am receiving the following warning:
Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 41
Here is the code:
$travel = array("Automobile", "Jet", "Ferry", "Subway");
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
<h4>Add more options (comma separated)</h4>
<form method="post" action="index2.php">
<input type="text" name="added" />
<?php
foreach ($travel as $t){
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />\n";
}
?>
<input type="submit" name="submit" value="Add" />
</form>
<?php
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
print_r ($travel);
?>
You're assigning $_POST["travel"], which is not an array but a string, to $travel. Turn it into an array first.
You are accessing $_POST["travel"] but it's not defined if you didn't submit the form. You need to check if it's a post request:
<?php
if(isset($_POST["travel"])){
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
}
print_r ($travel);
?>
$_POST is an array, $_POST['travel'] however is just an element unless its originating from a multiselect element.