Jump to content

Recommended Posts

I have put together a method to reboot my SIP phones.  I can use an array to feed the foreach and it works fine.  Now I need to take a step or two further.  I want to use the result of a query in the array.  The next step would be to instead of having all the extensions in the array  , to be able to select extensions from a form.  But for now getting the initial array is what I am needing help with.

//*****This doesn't work *******
//$phones = "SELECT extension FROM `extensions`IN(".implode(',',$array).")";

//******This works********
$phones  = array(201,202,204);

foreach ($phones as $phone)
{
$out = shell_exec("asterisk -rx 'sip notify CQ_Phone-Reboot $phone'");
echo str_replace("\n", "<br/>", $out);
}

Thanks

Link to comment
https://forums.phpfreaks.com/topic/308408-need-help-with-array/
Share on other sites

$phones = "SELECT extension FROM `extensions` where extension IN(".implode(',',$array).")"; 

or whatever your field is that matches the values in the array

Edited by cyberRobot
Removed quote, which included the entire post above.

Thanks for the reply.

Using your suggestion I am getting this:

Undefined variable: array in /var/www/html/cqadmin/Assign/restart.php on line 23 Warning: implode(): Invalid arguments passed in /var/www/html/cqadmin/Assign/restart.php on line 23

Line 23 is the new code.

 

Barand

Point taken

 

 

Here's all I have at the moment.  The table does populate with the correct info

$sql = "select extension from extensions";
$result = mysqli_query($link,$sql) or die(mysql_error());
echo "<table border='3'>
    <tr>
        <th>Extension #</th>
    </tr>";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['extension'] . "</td>";
             
    }
       
echo "</table>";

//$phones = "SELECT extension FROM extensions where extension IN(".implode(',',$array).")";
//$phones  = array(201,202,204);

foreach ($result as $phone)
{
$out = shell_exec("asterisk -rx 'sip notify CQ_Phone-Reboot $phone'");
echo str_replace("\n", "<br/>", $out);
}
?>

Thanks

3 minutes ago, elentz said:

echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";

In case this is more than just test code, the above code is missing the closing tag for the table row.

As for the error, $array isn't defined in your code. Assuming that you are looking for extensions 201, 201, and 204, try something like this

$extensions = array(201,202,204);
$sql = "SELECT extension FROM extensions where extension IN(".implode(',',$extensions).")";

Note that you'll need to run the query, like you did for the extensions HTML table. The foreach loop that runs through the phone numbers can be replaced with a while loop, like you have for the HTML table.

1 hour ago, elentz said:

...able to select extensions from a form...

I should mention that you'll want to use caution when you get to the step where you are populating the $extensions array using form input. Information supplied by a user should never be trusted. Forms, for example, can be tampered with, even ones using the POST method.

This did the trick!

$sql = "select extension from extensions";
$result = mysqli_query($link,$sql) or die(mysql_error());

while($row = mysqli_fetch_array($result))
{
    $out = shell_exec("asterisk -rx 'sip notify CQ_Phone-Reboot " . $row['extension'] . "'");
    //echo str_replace("\n", "<br/>", $out);         
    }

Thanks so much for the help !

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.