Help - Search - Members - Calendar
Full Version: Problem with arrays
Pixel2Life Forum > Help Section > PHP, ASP, MySQL, JavaScript and other Web/Database Programming Help
nowy
Hi everyone,

My name is Nowy and I'm new to the forum, but was wondering if anybody could help me out with a small issue I'm facing.

I currently have the code:

CODE
$query_games = "SELECT * FROM games ORDER BY name ASC";
$mysqlresult_games = mysql_query($query_games) or die('ERROR '.$query_games.' '.mysql_error());
while ($row_games = mysql_fetch_array($mysqlresult_games))
{
echo '<input type="checkbox" name="games[]" value="'.htmlentities(stripslashes($row_games['id'])).'"'; if (($gamescheck[0] == $row_games['id']) || ($gamescheck[1] == $row_games['id']) || ($gamescheck[2] == $row_games['id']) || ($gamescheck[3] == $row_games['id']) || ($gamescheck[4] == $row_games['id'])) { echo 'checked'; } echo '/> <img src="'.htmlentities(stripslashes($row_games['image'])).'" width="12" height="12" alt="" /> '.htmlentities(stripslashes($row_games['name'])).'<br />';

}



I was wondering if there was any quicker way of writing the following code as I need to go up to infinity:

CODE
if (($gamescheck[0] == $row_games['id']) || ($gamescheck[1] == $row_games['id']) || ($gamescheck[2] == $row_games['id']) || ($gamescheck[3] == $row_games['id']) || ($gamescheck[4] == $row_games['id'])) { echo 'checked'; }



Thanks!

Nowy
rc69
CODE
if(in_array($row_games['id'], $gamescheck)){ echo 'checked'; }

ref: http://php.net/manual/en/function.in-array.php
nowy
QUOTE (rc69 @ Aug 21 2009, 01:32 AM) *
CODE
if(in_array($row_games['id'], $gamescheck)){ echo 'checked'; }

ref: http://php.net/manual/en/function.in-array.php


Works great thanks a lot!

By the way, now that I have your attention, I have another query.
Is there anyway of adding for example: mysql_real_escape_string() to all $_GET, $_POST, $_COOKIE.... without actually manually putting them there ?

I had the following code:

CODE
$_POST = array_map('escape', $_POST);

function escape($str) { return mysql_real_escape_string($str); }


However with the code I previously asked help with, it returns the error:

Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in xxx\functions.php on line 9
derek.sullivan
try this instead:

CODE
function escape($str) { $str = mysql_real_escape_string($str); return $str; }
nowy
QUOTE (derek.sullivan @ Aug 21 2009, 03:10 PM) *
try this instead:

CODE
function escape($str) { $str = mysql_real_escape_string($str); return $str; }


Nice thanks!

But will this work automatically and for all of the gets, posts, cookies, sessions.... etc. ?

OR do i have to do:

CODE
$_POST = array_map('escape', $_POST);
$_GET = array_map('escape', $_GET);
$_COOKIE = array_map('escape', $_COOKIE);
function escape($str) { $str = mysql_real_escape_string($str); return $str; }
derek.sullivan
as long as $str is = to what you want to filter.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.