Jump to content

help with array


harkly

Recommended Posts

I am setting up a Select All on a page and am then passing the data to another page that will make modifications

to the DB. User will be able to Select as many as they want.

 

Data is in an array and I cannot figure out how to 'break' it so I can use each data.

 

I have the form part working and it is passing the info

 

Code for POST page

 

<?php

$my_array = $_POST['answers'];
print_r($my_array);
echo "<br>";

$info = explode(" ", $my_array);
echo "<br>";
echo $info[0];
echo $info[1];
echo $info[2];

echo "<br>";
echo "<br>";


foreach ($my_array as $values){
  echo $value."<br>";
}


?>

 

 

My output

 

Array ( [0] => 49 [1] => 46 [2] => 39 [3] => 36 )

 

Array

 

I have no idea what to do and cannot find anything online that helps me with splitting the Array ( [0] => 49 [1] => 46 [2] => 39 [3] => 36 )

 

 

Lots on working with this...

$colorList = array("red","green","blue","black","white");
foreach ($colorList as $value) {
  echo $value."<br>";
}

Can someone point me in a good direction?

Link to comment
https://forums.phpfreaks.com/topic/219501-help-with-array/
Share on other sites

This is a very basic message system.

 

I am pulling info from a DB in a while loop. The user then has the ability to Select All returned data or however many they want. Then there will be a few options they can have done. Delete, Mark as Unread, View.

 

I need to be able to run whatever option they have selected on all the Selected info.

 

My form is passing the $ids to another page which will execute the option. So if ids 49, 32, and 12 are selected then I need to be able to update each in the DB.

 

I have this working

 

$my_array = $_POST['answers'];
print_r($my_array);
echo "<br>";


echo "<br>";
$totalIDs = count($my_array);

for ( $i=0; $i < $totalIDs; $i++ ) {
    $query2 = ("UPDATE threads SET s_status=0 WHERE id='$my_array[$i]'");
    $result2 = mysql_query($query2) or die(mysql_error());
} // END for

 

If there is a better way let me know.

Link to comment
https://forums.phpfreaks.com/topic/219501-help-with-array/#findComment-1138427
Share on other sites

Depends on how you determine whether its a update, delete, etc...  here I use two submit buttons with the same name and different values:

 

switch($_POST['submit']) {
  case 'update':
    $query = "UPDATE threads SET s_status=0 WHERE id=";
    break;
    
  case 'delete':
    $query = "DELETE FROM threads WHERE id=";
    break;
}
$ids = array_map('mysql_real_escape_string', $_POST['answers']);

foreach($ids as $id) {
    $result = mysql_query($query.$id) or die(mysql_error());
}

 

Or without a loop this should work better (you really don't want queries in a loop):

 

$ids = implode(',', array_map('mysql_real_escape_string', $_POST['answers']));

switch($_POST['submit']) {
  case 'update':
    $query = "UPDATE threads SET s_status=0 WHERE id IN($ids)";
    break;
    
  case 'delete':
   $query = "DELETE FROM threads WHERE id IN($ids)";
   break;

  default:
     $query = '';
     break;
}
if($query) {
   $result = mysql_query($query) or die(mysql_error());
}

 

Just examples...

Link to comment
https://forums.phpfreaks.com/topic/219501-help-with-array/#findComment-1138490
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.