0ndre_
0ndre_

Reputation: 3631

Run php action for every element

I have a php script which retrieves data from mysql db. Everything works fine, but my problem is that this $result = $dao->joinedEvents($userId); returns an array of numbers and what I would like to do is to run this $secondResult = $dao->joinedEventsInfo($receivedIds); for every ID and this script I'm using right now returns data only for one ID.

This is part of my php script:

$userId = htmlentities($_REQUEST["userId"]);

$result = $dao->joinedEvents($userId); //This is getting the IDs array

if(!empty($result)) {


$receivedIds = $result["event_id"];
$ids = explode(",", $receivedIds);

foreach($ids as $id){
    $secondResult = $dao->joinedEventsInfo($id);
    if(!empty($secondResult)) {   
        $returnValue["finalResult"][] = $secondResult;    
    } else {    
        $returnValue["status"] = "error";   
        $returnValue["message"][] = "Could not find records for id" . $id;
    }
}


} else {
    $returnValue["status"] = "Empty error"; 
    $returnValue["message"] = "Could not find records";
}


$dao->closeConnection();
echo json_encode($returnValue);

And this is joinedEvents script:

public function joinedEvents($userId){

     $returnValue = array();

    $sql = "SELECT event_id from MyTable WHERE userId= '$userId' LIMIT 0 , 30";
      $statement = $this->conn->prepare($sql);
        if (!$statement)
            throw new Exception($statement->error);

        $statement->execute();

        $result = $statement->get_result();

         while ($myrow = $result->fetch_assoc()) 
         {
           $returnValue[] = $myrow;
         }

         return $returnValue;
    }

This is joinedEventsInfo script:

public function joinedEventsInfo($eventId){

     $returnValue = array();

     $sql = "SELECT * FROM Events WHERE eventId = '$eventId' LIMIT 0 , 30";

    $statement = $this->conn->prepare($sql);
    if (!$statement)
        throw new Exception($statement->error);

    $statement->execute();

    $result = $statement->get_result();

     while ($myrow = $result->fetch_assoc()) 
     {
       $returnValue[] = $myrow;
     }

     return $returnValue;

 }

Edit: Tha reason I need this is that I have two tables. In the first one I have just IDs and in the second one I have info. So first I need to get the IDs and then I need to get data for every ID I have just received.

Thank you very much , I'm totally stuck.

Upvotes: 0

Views: 49

Answers (2)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

Based on the updated code snippets and the discussion below, it is found that $result is indeed an array, and the solution is:

$userId = htmlentities($_REQUEST["userId"]);
$result = $dao->joinedEvents($userId);

if(count($result)){
    foreach($result as $array){
        $event_id = $array['event_id'];
        $secondResult = $dao->joinedEventsInfo($event_id);
        if(!empty($secondResult)) {  
            $returnValue["finalResult"][] = $secondResult;    
        } else {    
            $returnValue["status"] = "error";  
            $returnValue["message"][] = "Could not find records for id: " . $event_id;
        }
    }
}else {
    $returnValue["status"] = "Empty error";
    $returnValue["message"] = "Could not find records";
}
$dao->closeConnection();
echo json_encode($returnValue);

Upvotes: 2

tristansokol
tristansokol

Reputation: 4271

Have you tried array_map()? That would allow you to call a php function on each member of an array easily.

Another way would be to use the common while ($row = mysql_fetch_array($result)) which would execute the code in the while loop for each row of your returned results. Note, you will likely have to change the mysql_fetch_array to something specific for your SQL connection.

Upvotes: 1

Related Questions