breaking down an array
Moderator: General Moderators
-
mrgrinch12
- Forum Newbie
- Posts: 16
- Joined: Mon Jun 07, 2010 10:20 am
breaking down an array
I have a form that produces an array when submitted and I need to strip out the use able data for an update query. The array contains the number of the row needing to be updated and what it needs to be updated with. Here is an example of what I need from the array:
1182 = Active
here is the output of the array:
array(21) { [2028]=> string(6) "Active" [1176]=> string(6) "Active" [1331]=> string(6) "Active" [842]=> string(6) "Active" [7816]=> string(6) "Active" [2769]=> string(5) "Bench" [2392]=> string(6) "Active" [1916]=> string(6) "Active" [1976]=> string(6) "Active" [2131]=> string(6) "Active" [313]=> string(6) "Active" [1792]=> string(6) "Active" [1803]=> string(6) "Active" [1859]=> string(6) "Active" [1714]=> string(6) "Active" [1556]=> string(6) "Active" [1321]=> string(6) "Active" [2394]=> string(6) "Active" [1268]=> string(6) "Active" [761]=> string(6) "Active" ["submit"]=> string(6) "submit" } Array ( [0] => )
1182 = Active
here is the output of the array:
array(21) { [2028]=> string(6) "Active" [1176]=> string(6) "Active" [1331]=> string(6) "Active" [842]=> string(6) "Active" [7816]=> string(6) "Active" [2769]=> string(5) "Bench" [2392]=> string(6) "Active" [1916]=> string(6) "Active" [1976]=> string(6) "Active" [2131]=> string(6) "Active" [313]=> string(6) "Active" [1792]=> string(6) "Active" [1803]=> string(6) "Active" [1859]=> string(6) "Active" [1714]=> string(6) "Active" [1556]=> string(6) "Active" [1321]=> string(6) "Active" [2394]=> string(6) "Active" [1268]=> string(6) "Active" [761]=> string(6) "Active" ["submit"]=> string(6) "submit" } Array ( [0] => )
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: breaking down an array
You're going to have to be more descriptive about what you're doing/need.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
mrgrinch12
- Forum Newbie
- Posts: 16
- Joined: Mon Jun 07, 2010 10:20 am
Re: breaking down an array
I need to extract the key and the value from the array as a pair to match up the key with the row in the database so that I can update the value into the db.
Would it help if I posted the two pages of code that led me to here?
Would it help if I posted the two pages of code that led me to here?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: breaking down an array
There are may ways to do that but just based on your first post I will suggest this:
Code: Select all
foreach($_POST['form_array'] as $key => $value) {
echo $key . '=' . $value;
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
mrgrinch12
- Forum Newbie
- Posts: 16
- Joined: Mon Jun 07, 2010 10:20 am
Re: breaking down an array
Code: Select all
<?php
session_start();
require('/conn_ssfhl.php');
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_DetailRS1 = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_DetailRS1 = $_SESSION['MM_Username'];
}
mysql_select_db($database_conn_ssfhl, $conn_ssfhl);
$query_DetailRS1 = sprintf("SELECT PlayerID, FanID, Type, InjuryID, Player, StatusID, StatusChangeChoice, CurrentWeek, GMEmail FROM tblplayers WHERE GMEmail = %s ORDER BY tblplayers.Type Asc", GetSQLValueString($colname_DetailRS1, "text"));
$DetailRS1 = mysql_query($query_DetailRS1, $conn_ssfhl) or die(mysql_error());
$row_DetailRS1 = mysql_fetch_assoc($DetailRS1);
$totalRows_DetailRS1 = mysql_num_rows($DetailRS1);
$colname_rsGMDetail = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_rsGMDetail = $_SESSION['MM_Username'];
}
mysql_select_db($database_conn_ssfhl, $conn_ssfhl);
$query_rsGMDetail = sprintf("SELECT GMFirst, GMLast FROM tbluseradmin WHERE GMEmail = %s", GetSQLValueString($colname_rsGMDetail, "text"));
$rsGMDetail = mysql_query($query_rsGMDetail, $conn_ssfhl) or die(mysql_error());
$row_rsGMDetail = mysql_fetch_assoc($rsGMDetail);
$totalRows_rsGMDetail = mysql_num_rows($rsGMDetail);
// Count rows in table
$count=mysql_num_rows($DetailRS1);
?>
<?php
// Post variables
//$player = $_POST['hideplayerID'];
//$playerid = $_POST['PlayerID'];
//$select_id = $row['PlayerID'];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>form</title>
</head>
<body bgcolor="#FFFFFF">
<form id="frmupdate" name="frmupdate" method="post" action="update2.php">
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center" bgcolor="#FFCC00"><strong>ID</strong></td>
<td align="center" bgcolor="#FFCC00">Position</td>
<td align="center" bgcolor="#FFCC00">Player</td>
<td align="center" bgcolor="#FFCC00">Health</td>
<td align="center" bgcolor="#FFCC00">Current Status</td>
<td align="center" bgcolor="#FFCC00"><strong>Change Status</strong></td>
</tr>
<?php
// Fetch record rows in $DetailRS1 by while loop and put them into $row.
while($row=mysql_fetch_assoc($DetailRS1)){
?>
<tr>
<td bgcolor="#FFFFCC"><? echo $row['PlayerID']; ?></td>
<td bgcolor="#FFFFCC"><? echo $row['Type']; ?></td>
<td bgcolor="#FFFFCC"><? echo $row['Player']; ?></td>
<td bgcolor="#FFFFCC"><? echo $row['InjuryID']; ?></td>
<td bgcolor="#FFFFCC"><? echo $row['StatusID']; ?></td>
<td bgcolor="#FFFFCC"><label>
<select name=<?=$row['PlayerID']?>>
<option value="Active" selected="selected">Active</option>
<option value="Bench">Bench</option>
</select>
</label></td>
</tr>
<tr> </tr>
<?php } // End while loop. ?>
</table>
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
</body>
</html><?php
?>
//update page code starts here
<?php require_once('../conn_ssfhl.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>update</title>
</head>
<body>
<?php
// If receive Submit button variable.
if($_POST['submit']){
// Select all data records in table "tblplayers" and put them into $result.
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
# Script 2.5 - update.php
//Validate the status and combat Magic Quotes, if neccessary.
if (!empty($_REQUEST['select'])) {
$select = stripslashes($_REQUEST['select']);
} else {
$select = NULL;
echo '<p>You forgot to change your player status!</p>';
}
//If everything else is okay, print the message
echo "<p>Thank you</p>";
}
else {//Selection was not made on form.
echo "<p>Please go back and complete your selections.</p>";
}
mysql_select_db($database_conn_ssfhl, $conn_ssfhl);
$query_result = "SELECT PlayerID FROM tblplayers ORDER BY PlayerID ASC";
$result = mysql_query($query_result, $conn_ssfhl) or die(mysql_error());
$row_result = mysql_fetch_assoc($result);
$totalRows_result = mysql_num_rows($result);
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
else {
// Fetch record rows in $result by while loop and put them into $row.
while($row = mysql_fetch_assoc($result)){
//start a counter in order to number the input fields for each record
for($i=0;$i<$count;$i++){
// Update field "StatusChangeChoice", matching with "PlayerID" value by while loop.
foreach ($_POST as $my_player_id) {
$query_update = "UPDATE tblplayers SET StatusChangeChoice = '".$_POST[$my_player_id]."' where PlayerID = '$my_player_id'";
mysql_query($query_update);
}
if (!$update) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query_update;
die($message);
}
}
}
}
echo "--- Update Complete ---";
echo "This is the query : ". $query_update;
?>
</body>
</html>
<?php
echo $my_player_id;
print_r($my_player_id);
var_dump($_POST);
mysql_free_result($result);?>
?>- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: breaking down an array
No offense, but that is a garbled mess and I still have no idea what you're trying to do.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
mrgrinch12
- Forum Newbie
- Posts: 16
- Joined: Mon Jun 07, 2010 10:20 am
Re: breaking down an array
I have a form that allows users to use a drop down to select either Active or Bench status for their players based on the player ID. The player ID is bound to the drop down box and when the form is posted an array is created. The array key is the player ID and the value is the choice they selected on the submitted form. the posting from the submit produces these results for the array:
Array ( [2028] => Active [1176] => Active [1331] => Active [842] => Active [7816] => Active [2769] => Active [2392] => Active [1916] => Active [1976] => Active [2131] => Active [313] => Active [1792] => Active [1803] => Active [1859] => Active [1714] => Active [1556] => Active [1321] => Active [2394] => Active [1268] => Active [761] => Active [submit] => submit ) ?>
From this array, I need to extract the player ID 2028 and the select Active and put them into a usable format to perform an update query to the mySQL database. The basic update query would look something like this:
UPDATE table_name SET column_name_status = "select data from array" WHERE column_name_PlayerID = "player id from array
Does that help?
Array ( [2028] => Active [1176] => Active [1331] => Active [842] => Active [7816] => Active [2769] => Active [2392] => Active [1916] => Active [1976] => Active [2131] => Active [313] => Active [1792] => Active [1803] => Active [1859] => Active [1714] => Active [1556] => Active [1321] => Active [2394] => Active [1268] => Active [761] => Active [submit] => submit ) ?>
From this array, I need to extract the player ID 2028 and the select Active and put them into a usable format to perform an update query to the mySQL database. The basic update query would look something like this:
UPDATE table_name SET column_name_status = "select data from array" WHERE column_name_PlayerID = "player id from array
Does that help?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: breaking down an array
There are better ways, but to use your existing code and f you are going to do all of the values in the array then you would use the foreach as I showed:mrgrinch12 wrote:I have a form that allows users to use a drop down to select either Active or Bench status for their players based on the player ID. The player ID is bound to the drop down box and when the form is posted an array is created. The array key is the player ID and the value is the choice they selected on the submitted form. the posting from the submit produces these results for the array:
Array ( [2028] => Active [1176] => Active [1331] => Active [842] => Active [7816] => Active [2769] => Active [2392] => Active [1916] => Active [1976] => Active [2131] => Active [313] => Active [1792] => Active [1803] => Active [1859] => Active [1714] => Active [1556] => Active [1321] => Active [2394] => Active [1268] => Active [761] => Active [submit] => submit ) ?>
From this array, I need to extract the player ID 2028 and the select Active and put them into a usable format to perform an update query to the mySQL database. The basic update query would look something like this:
UPDATE table_name SET column_name_status = "select data from array" WHERE column_name_PlayerID = "player id from array
Does that help?
Code: Select all
foreach($_POST as $playerid => $status) {
//UPDATE table_name SET column_name_status = '$status' WHERE column_name_PlayerID = '$playerid'
}[text]<select name="players[<?=$row['PlayerID']?>]">
<option value="Active" selected="selected">Active</option>
<option value="Bench">Bench</option>
</select>[/text]
And then:
Code: Select all
foreach($_POST['players'] as $playerid => $status) {
//UPDATE table_name SET column_name_status = '$status' WHERE column_name_PlayerID = '$playerid'
}mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
mrgrinch12
- Forum Newbie
- Posts: 16
- Joined: Mon Jun 07, 2010 10:20 am
Re: breaking down an array
Thanks for all the help. I may have to start all over as I tried both options an am now getting this message:
You forgot to change your player status!
Thank you
Invalid query: Whole query:
I also cannot get any echo to show for the key and value created in the for each statement
You forgot to change your player status!
Thank you
Invalid query: Whole query:
I also cannot get any echo to show for the key and value created in the for each statement
Code: Select all
//start a counter in order to number the input fields for each record
for($i=0;$i<$count;$i++){
// Update field "StatusChangeChoice", matching with "PlayerID" value by while loop.
//foreach ($_POST as $my_player_id) {
//$query_update = "UPDATE tblplayers SET StatusChangeChoice = '".$_POST[$my_player_id]."' where PlayerID = '$my_player_id'";
foreach($_POST as $playerid => $status) {
$query_update = "UPDATE tblplayers SET StatusChangeChoice = '$status' WHERE PlayerID = '$playerid'";
}
mysql_query($query_update);
}
if (!$update) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query_update;
die($message);
}
}
}
// }
echo "--- Update Complete ---";
echo "This is the query : ". $query_update;
?>
</body>
</html>
<?php
//echo $my_player_id;
print_r($_POST);
var_dump($_POST);
echo $playerid;
echo $status;
mysql_free_result($result);?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: breaking down an array
You never assigned $update... which is why you should always develop with error_reporting(E_ALL); at least, as you'd have been notified you are using an uninitialized variable.
Basically,
should be
Basically,
Code: Select all
mysql_query($query_update);Code: Select all
$update = mysql_query($query_update);-
mrgrinch12
- Forum Newbie
- Posts: 16
- Joined: Mon Jun 07, 2010 10:20 am
Re: breaking down an array
John,
Nice catch...tried it and no change in message or result
Nice catch...tried it and no change in message or result