Jump to content

php code help with arrays


johnnytz1

Recommended Posts

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?

[code]$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);[/code]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--quoteo(post=362502:date=Apr 7 2006, 07:58 AM:name=eric1235711)--][div class=\'quotetop\']QUOTE(eric1235711 @ Apr 7 2006, 07:58 AM) [snapback]362502[/snapback][/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
Link to comment
Share on other sites

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

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

[code]$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);

?>[/code]

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
Link to comment
Share on other sites

I would recomend the following.

[code]
$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);
[/code]

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.

[code]
for ($i=0; $i < count($arr_b); $i++){
[/code]

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;

[code]
$size = count($arr_b);
for ($i=0; $i < $size; $i++){
[/code]

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--quoteo(post=363111:date=Apr 9 2006, 03:05 PM:name=johnnytz1)--][div class=\'quotetop\']QUOTE(johnnytz1 @ Apr 9 2006, 03:05 PM) [snapback]363111[/snapback][/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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.