Reply
Thread Tools Display Modes
Unread 08-01-13, 09:12 AM   #1
Bl4ckSh33p
 
Bl4ckSh33p's Avatar
Mod Author - Click to view Mods
Join Date: Jul 2013
Posts: 12
Exclamation Looking for someone with PHP experience (foreach and array problem)

Hi! I only know a few basic things about PHP and tried to get this to work for hours but it just does not work.
So I want to ask if someone here might be experienced with PHP. If you know it this should only take a minute or two for you to figure out.

I have a nested array. Its just a list like this

array[0]["Name"]="Alex";
array[0]["Serial"]="12345";
array[0]["NIC"]="1dA21n";
array[1]["Name"]="Roland";
array[1]["Serial"]="1337";
array[1]["NIC"]="uR0ck";
and so on...

and I want to compare the entered value submitted with HTTPPost to all array entries with a foreach loop. But I can't figure out how to write this loop properly.

The original code:
<?
// Check for post data
if ($_POST)
{
// Initialize user array in format "Username"=>"Serial"
$user_array = array(
"John Doe"=>"abc-def-ghi",
"Jane Smith"=>"123-456-789"
);

// Get info from post variables
$user = $_POST['user'];
$serial = $_POST['serial'];
$installed = $_POST['installed'];

// Step through the user array
foreach($user_array as $username=>$serialnumber)
{
// Compare current item to the posted information
if(($user==$username) AND ($serial==$serialnumber))
{
// The current user matched, return info and exit
echo md5($user).md5($serial).'::'.$installed;
exit;
}
}

// If this script makes it this far, nothing matched, return error code
echo -1;
}
else
{
// no post data was sent, return error code
echo -1;
}
?>
I changed the array and added another POST variable.
I tried several things from the php manual and php tutorial sites like:

foreach ($user_array as $type => $properties) {
// $properties = $GodArray[$type]
foreach ($properties as $property => $value) {
// $value = $GodArray[$type][$property]
//echo $property, "=", $value, "<br />";
if(($user==$username) AND ($serial==$serialnumber) AND ($nic==$nicid)
{
//The current user matched, return info and exit
echo md5($user).md5($serial).md5($nic).'::'.$installed;
exit;
}

}
}

or:

/*
foreach ($user_array as $nr => $inhalt)
{
$username[$nr] = ( $inhalt['User'] );
$serialnumber[$nr] = ( $inhalt['Serial'] );
$nicid[$nr] = ( $inhalt['NIC'] );
echo $username.$serialnumber.$nicid;
}
*/

or:
// Step through the user array
//foreach($user_array as ($username, $serialnumber, $nicid))

//foreach ($user_array as $v1) {
//foreach ($v1 as $v2) {
// if(($user==$username) AND ($serial==$serialnumber) AND ($nic==$nicid)
// {
// The current user matched, return info and exit
// echo md5($user).md5($serial).md5($nic).'::'.$installed;
// exit;
// }
//}
//}

//{
// Compare current item to the posted information
//if(($user==$username) AND ($serial==$serialnumber) AND ($nic==$nicid)
//{
// The current user matched, return info and exit
// echo md5($user).md5($serial).md5($nic).'::'.$installed;
// exit;
//}
//}

But it puts out everything in one line (all data from every array entry) or I get an syntax error with missing , ( or {.

Does anyone know how to do this? Any help would be very much appreciated.

Last edited by Bl4ckSh33p : 08-01-13 at 12:04 PM.
Bl4ckSh33p is offline   Reply With Quote
Unread 08-02-13, 10:18 AM   #2
y2bcrazy
 
y2bcrazy's Avatar
Mod Author - Click to view Mods
Join Date: Jul 2013
Posts: 3
Guides

I personally cannot help you 100% with this just yet since i'm loaded on work and am currently building my own forum.
However, here is a very good guide i found which helps you visualize and contextualize your work

http://net.tutsplus.com/tutorials/ph...-from-scratch/
y2bcrazy is offline   Reply With Quote
Unread 08-03-13, 05:17 AM   #3
Bl4ckSh33p
 
Bl4ckSh33p's Avatar
Mod Author - Click to view Mods
Join Date: Jul 2013
Posts: 12
Hi. Thanks for sharing but I could not find much helpful info in it.
But I have a solution for my problem now.

Code:
foreach ($user_array as $nr => $inhalt) { 
        // Compare current item to the posted information 
        if($user == $inhalt["User"] AND $serial == $inhalt["Serial"]) { 
            // The current user matched, return info and exit 
            echo md5($user).md5($serial).'::'.$installed; 
            exit; 
        }  
}
Bl4ckSh33p is offline   Reply With Quote
Reply

Go BackCube World Mods » Site Forums » Chit-Chat » Looking for someone with PHP experience (foreach and array problem)

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off