Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Question 1

Status
Not open for further replies.

iranor

Programmer
Joined
Jun 17, 2004
Messages
174
Location
CA
Is there a way to create an array from multiple arrays?

Here , I have to query 2 different tables and get data from a field.
The code :

$users_list = $this->db->query_fetch_assoc("SELECT player FROM friend_list WHERE owner = '$g_member_id' AND mutual = '1'");
$users_list = $this->db->query_fetch_assoc("SELECT members FROM alliance WHERE members = '$thisusername'");
$users_list = explode(';',$msg_to);

After , I have this :

foreach($users_list as $users){

The code above should make an array from the 3 different array , then it should be an array that can be used for foreach.

Above is all my code. This code is used in a mail system. If user type * in 'to' field , this sends the message to everybody in a certain table.
If he type fl , it does same thing , but for another table.

Also , I would like to makes it possible to use ; to separate users , while including different option 'send to all' like the fl and *.

The full code is :

if(strstr($msg_to, '*')){
$thisusername = $this->get_username_from_id($g_member_id);
$users_list = $this->db->query_fetch_assoc("SELECT members FROM alliance WHERE members = '$thisusername'");
$users_list = explode(';',$msg_to);
}
elseif(strstr($msg_to, 'fl'))
{
$users_list = $this->db->query_fetch_assoc("SELECT player FROM friend_list WHERE owner = '$g_member_id' AND mutual = '1'");
$users_list = explode(';',$msg_to);
}
elseif(strstr($msg_to, 'all'))
{
$users_list = $this->db->query_fetch_assoc("SELECT members FROM alliance WHERE members = '$thisusername'");
$users_list = $this->db->query_fetch_assoc("SELECT player FROM friend_list WHERE owner = '$g_member_id' AND mutual = '1'");
$users_list = explode(';',$msg_to);
}

foreach($users_list as $users){
....
 
As I see it, you're not creating an array from multiple arrays, as the block of code you've posted, unless wrapped in some kind of loop, will only run once.

Also, I don't understand what you're trying to do in these lines:
$users_list = $this->db->query_fetch_assoc([somequery]);
$users_list = explode(';',$msg_to);


What I would do is something along the lines of:

Code:
$recipient_array = explode (';', $msg_to);
$recipient_list = array();
foreach ($recipient_array as $recipient)
{
   switch ($recipient)
   {
      case '*':
         $thisusername = $this->get_username_from_id($g_member_id);
         $users_expansion = $this->db->query_fetch_assoc("SELECT members FROM alliance WHERE members = '$thisusername'");
         foreach ($users_expansion as $expand)
         (
            if (!in_array ($expand, $recipient_list))
            {
               $recipient_list[] = $expand;
            }
         }
         break;

      case 'fl':
         $users_expansion = $this->db->query_fetch_assoc("SELECT player FROM friend_list WHERE owner = '$g_member_id' AND mutual = '1'");
         foreach ($users_expansion as $expand)
         (
            if (!in_array ($expand, $recipient_list))
            {
               $recipient_list[] = $expand;
            }
         }
         break;

      case 'all':
         $users_expansion = $this->db->query_fetch_assoc("SELECT members FROM alliance WHERE members = '$thisusername'");
         foreach ($users_expansion as $expand)
         (
            if (!in_array ($expand, $recipient_list))
            {
               $recipient_list[] = $expand;
            }
         }
         $users_expansion = $this->db->query_fetch_assoc("SELECT player FROM friend_list WHERE owner = '$g_member_id' AND mutual = '1'");
         foreach ($users_expansion as $expand)
         (
            if (!in_array ($expand, $recipient_list))
            {
               $recipient_list[] = $expand;
            }
         }
         break;

      default:
         $recipient_list[] = $recipient;
   }
}

The above code assumes you're going to enter a recipient string like "someuser@somedomain.fu;fl;*;all". It will split the string at the semicolons into an array, then deal with each element of the array separately. It produces another array, called $recipient_list, which contains all the usernames and wildcard expansions.

NOTICE: I do not have your classes or data to play with, so the probability that the above code will work without modifications is very close to zero.

One assumption I made with this code is that the class method db->query_fetch_assoc() returns in one invocation an array of all records from the database resultset created by the provided query. If it does not, additional loops will have to be added to the above code.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks!!! this is perfect. You are really good.

One more thing : The query_fetch_assoc returns an array. In the friend_list table , I have 3 values. table friend_list :

id - owner - player - mutual
1 - 1 - 1 - 1
2 - 1 - 2 - 1
3 - 1 - 3 - 1

Why does it only sends it to the id 1 , and not the 3 ?

case 'fl':
$users_expansion = $this->db->query_fetch_assoc("SELECT player FROM friend_list WHERE owner = '$g_member_id' AND mutual = '1'");
foreach ($users_expansion as $expand)
{
if (!in_array ($expand, $recipient_list))
{
$recipient_list[] = $expand;
}
}
break;
 
I found , I replaced foreach with a while , thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top