Jump to content


Photo

php code help with arrays


  • Please log in to reply
7 replies to this topic

#1 johnnytz1

johnnytz1
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 April 2006 - 11:39 AM

Here is my php code but I am unsure that this will work. I am trying to create a array and pass it a variable that checks what is stored in a database. Is this the correct way to do this?

$my_arr = array();
$my_arr = $_POST['arr'];

$link = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

foreach ($my_arr) {
    $result = mysql_query("SELECT * FROM coordsTaken WHERE xyPOS = '$my_arr'");
    $result = mysql_query("SELECT * FROM coordsReserved WHERE xyPOS = '$my_arr'");
}
$good = "good";
$bad = "duplicates";
if (mysql_num_rows($result) > 0) {
    $rstring .="&message=".$bad."&";
} else {
    $rstring .="&message=".$good."&";
}

echo $rstring."&";
mysql_close($link);


#2 eric1235711

eric1235711
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • LocationSão Paulo - Brazil

Posted 07 April 2006 - 12:58 PM

that foreach will not work:

it must be something like

foreach($my_arr as $my_key => $my_value)

or

foreach($my_arr as $my_value)


and inside the foreach loop, all the results will be overwriten and that final IF will check only the numrows of the last query.


diy XP
Programming is like building your own universe...
But let me ask you something:
Your world... It´s cool? Or it sucks?

#3 johnnytz1

johnnytz1
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 07 April 2006 - 01:12 PM

[!--quoteo(post=362502:date=Apr 7 2006, 07:58 AM:name=eric1235711)--][div class=\'quotetop\']QUOTE(eric1235711 @ Apr 7 2006, 07:58 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
that foreach will not work:

it must be something like

foreach($my_arr as $my_key => $my_value)

or

foreach($my_arr as $my_value)
and inside the foreach loop, all the results will be overwriten and that final IF will check only the numrows of the last query.
diy XP
[/quote]


ty

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 April 2006 - 01:56 PM

I would recommmend you to change this:
$result = mysql_query("SELECT * FROM coordsTaken WHERE xyPOS = '$my_arr'");
    $result = mysql_query("SELECT * FROM coordsReserved WHERE xyPOS = '$my_arr'");
to the following:
$result = mysql_query("SELECT ct.*, cr.* FROM coordsTaken ct, coordsReserved cr WHERE ct.xyPOS = '$my_arr' AND cr.xyPOS = '$my_arr'");
As currently your secound query is over ridding the result of your first query!

#5 johnnytz1

johnnytz1
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 08 April 2006 - 12:46 AM

Ok i been doing a little editing needs some help im a noob.

$link =mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

$arr_b = array("x500y500","x510y500");
$arr_r = array("x500y510","x500y520","x510y510","x510y520");




for ($i=0; $i < count($arr_b); $i++){
    $coord_taken .= mysql_query("SELECT * FROM coordsTaken WHERE xyPOS = '$arr_b[$i]'");
    $coord_taken .= mysql_query("SELECT * FROM coordsReserved WHERE xyPOS = '$arr_b[$i]'");
    echo "Already bought";

}


for ($i=0; $i < count($arr_r); $i++){
    $coord_reserv .= mysql_query("SELECT * FROM coordsReserved WHERE xyPOS = '$arr_r[$i]'");
    $coord_reserv .= mysql_query("SELECT * FROM coordsReserved WHERE xyPOS = '$arr_r[$i]'");
    echo "Already reserved";
}


mysql_close($link);

?>

Here is what im trying to do. I have created two arrays with identical values that are in the database. I want to step through the database and select the matching rows. If there is a matching row display a message. I know i need a if statement in there. But i am unsure if the select statements are correct.

thanks again

#6 IceHawk

IceHawk
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 08 April 2006 - 01:35 AM

I would recomend the following.

$link =mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

$arr_b = array("x500y500","x510y500");
$arr_r = array("x500y510","x500y520","x510y510","x510y520");

foreach($arr_b as $coord){
    $res = mysql_query("select ct.*, cr.* from coordsTaken ct, coordsReserved cr where cr.xyPOS=ct.xyPOS and ct.xyPOS='$coord'");
    if(!$res || !mysql_num_rows($res)){
        echo "Bad Coordinate";
    }else echo "Good Coordinate";
}
foreach($arr_r as $coord){
    $res = mysql_query("select ct.*, cr.* from coordsTaken ct, coordsReserved cr where cr.xyPOS=ct.xyPOS and ct.xyPOS='$coord'");
    if(!$res || !mysql_num_rows($res)){
        echo "Bad Coordinate";
    }else echo "Good Coordinate";
}

mysql_close($link);

Combining the queries will save on the query count and is much faster. The more you can do in a query, the better. It's much more efficient to have the DB do the work then the code. foreach loops are also much faster than for loops. The way you structured the for loop is also inefficient.

for ($i=0; $i < count($arr_b); $i++){

he problem with this is that after every iteration, count($arr_b) will be re-evaluated. So if the array is very large, you will be re-evaluating a constant value needlessly (unless the size of the array is modified in the loop). Much more efficient to do something such as;

$size = count($arr_b);
for ($i=0; $i < $size; $i++){

That way count($arr_b) is only evaluated once. With small arrays this isn't an issue, but if the array is large, it can add processing time.
[a href="http://www.devtext.com" target="_blank"]DevText a community for all languages[/a]

#7 johnnytz1

johnnytz1
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 09 April 2006 - 08:05 PM

thanks so far.

Here is what I want to do.

My resCoords table and buyCoords table have opposite values.
Im trying to use the select statement to select a match from either table.

Is this possible with one select statement as we were trying to do? or would i have to concat the result from the queries? how would that go? thanks.. jtz1 lil bro

#8 johnnytz1

johnnytz1
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 10 April 2006 - 10:35 PM

[!--quoteo(post=363111:date=Apr 9 2006, 03:05 PM:name=johnnytz1)--][div class=\'quotetop\']QUOTE(johnnytz1 @ Apr 9 2006, 03:05 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thanks so far.

Here is what I want to do.

My resCoords table and buyCoords table have opposite values.
Im trying to use the select statement to select a match from either table.

Is this possible with one select statement as we were trying to do? or would i have to concat the result from the queries? how would that go? thanks.. jtz1 lil bro
[/quote]

anyone?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users