Re: Foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 18, 2008 2:55 PM, Pastor Steve <smarquez@xxxxxxxxxxxxxxx> wrote:
>
>  Here is the output, thanks!
>
>  Array ( [page_name] => [image] => spacer.gif [text_head] => [name] => Array
> ( [0] => Holiday Inn [1] => The Rib Room [2] => Winner ) [order] => Array (
> [0] => 3 [1] => 3 [2] => 3 ) [add-page] => Create Page )
>
>  --
>  Steve M.
>
>
>
>  on 1/18/08 1:43 PM Eric Butera (eric.butera@xxxxxxxxx) wrote:
>
>
> On Jan 18, 2008 2:12 PM, Pastor Steve <smarquez@xxxxxxxxxxxxxxx> wrote:
>  >
>  >  I used the following code from Wolf and it did not work:
>  >
>  >
>  >  <?php
>  >  // First check to make sure you are getting both fields
>  >  if(isset($_POST['name']) && is_array($_POST['name']) &&
>  > isset($_POST['order']) && is_array($_POST['order']))
>  >  {
>  >   // Now assign them to easier to play with variables
>  >   $names=$_POST['name'];
>  >   $orders=$_POST['orders'];
>  >   // This tests for the same number of items as names
>  >   if (count($names) == count($orders))
>  >   {
>  >    $i=0;
>  >    while($i<=count($names))
>  >    {
>  >     $update_data = "UPDATE sections SET `order` = '$orders[$i]' WHERE
> name =
>  > '$names[$i]'";
>  >     $response = mysql_query( $update_data, $cnx );
>  >     if(mysql_error()) die ('database error<br>'.mysql_error());
>  >    }
>  >   }
>  >  }
>  >  ?>
>  >
>  >  Can you tell me why it didn't work? It just hangs as soon as the page is
>  > called. I know that both name and order are arrays. They are working
>  > separately.
>  >
>  >  Thanks for any help.
>  >
>  >  --
>  >  Steve M.
>  >
>  >
>  >
>  >
>  >
>  >  on 1/17/08 5:05 PM Eric Butera (eric.butera@xxxxxxxxx) wrote:
>  >
>  >
>  >
>  > On Jan 17, 2008 5:57 PM, Nathan Nobbe <quickshiftin@xxxxxxxxx> wrote:
>  >  > there are a few different issues here; first of all; are you sure
>  >  > $_POST['name']
>  >  > and $_POST['order'] are even arrays?
>  >
>  >  To check try this right above your saving code block:
>  >
>  >  echo '<pre>';
>  >  print_r($_POST);
>  >
>  >  After you figure out if your data is right we're going to have to talk
>  >  about input validation and sql injection.
>  >
>  >
>
>  If it is just hanging when you load the page I'm going to guess that
>  there is an issue with the while() statement.  If you delete all of
>  that and just do the print_r($_POST) and show me the output of that
>  I'll be able to help you further.  I need to see what the arrays
>  you've created in the post look like before I can write you a
>  statement to parse and use them.
>
>

Keep in mind that your name and order values really should be using
their primary key value.  Combining them could mess up if you don't
choose both options in the correct order.  I'll leave that to you to
figure out.


<?php
/*
Array (
	[page_name] =>
	[image] => spacer.gif
	[text_head] =>
	[name] => Array (
		[0] => Holiday Inn
		[1] => The Rib Room
		[2] => Winner
	)
	[order] => Array (
		[0] => 3
		[1] => 3
		[2] => 3
	)
	[add-page] => Create Page
)
*/

$_POST = array(
	'page_name' => '',
	'image'		=> 'spacer.gif',
	'text_head'	=> '',
	'name'		=> array(
		0 => 'Holiday Inn',
		1 => 'The Rib Room',
		2 => 'Winner'
	),
	'order'		=> array(
		0 => 3,
		1 => 3,
		2 => 3
	),
	'add-page' 	=> 'Create Page'
);

function super_duper_escaper($value, $db) {
	if (!get_magic_quotes_gpc()) {
		$value = mysql_real_escape_string($value, $db);
	}
	return $value;
}

if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
	
	$cnx = mysql_connect();
// handle this somehow too
	
	if (isset($_POST['name'], $_POST['order'])) {
		$_sql = "UPDATE sections SET `order` = %d WHERE name = '%s'";
		foreach (array_combine($_POST['name'], $_POST['order']) as $name => $order) {
			$sql = sprintf(
				   $_sql,
				   (int)$order,
				   super_duper_escaper($name, $cnx)
			);	
			$result = mysql_query($sql, $cnx);
			if (!$result) {
				die("Handle this gracefully");
			}
		}
	}

}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux