elentz Posted February 28, 2019 Share Posted February 28, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/ Share on other sites More sharing options...
taquitosensei Posted February 28, 2019 Share Posted February 28, 2019 (edited) $phones = "SELECT extension FROM `extensions` where extension IN(".implode(',',$array).")"; or whatever your field is that matches the values in the array Edited February 28, 2019 by cyberRobot Removed quote, which included the entire post above. Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564921 Share on other sites More sharing options...
Barand Posted February 28, 2019 Share Posted February 28, 2019 To use the results of a query you first have to execute the query (not just define a string with incorrect query syntax) Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564924 Share on other sites More sharing options...
elentz Posted February 28, 2019 Author Share Posted February 28, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564926 Share on other sites More sharing options...
cyberRobot Posted February 28, 2019 Share Posted February 28, 2019 What does your code look like now? Is $array defined...before the query? Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564928 Share on other sites More sharing options...
elentz Posted February 28, 2019 Author Share Posted February 28, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564929 Share on other sites More sharing options...
cyberRobot Posted February 28, 2019 Share Posted February 28, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564930 Share on other sites More sharing options...
cyberRobot Posted February 28, 2019 Share Posted February 28, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564932 Share on other sites More sharing options...
elentz Posted February 28, 2019 Author Share Posted February 28, 2019 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/308408-need-help-with-array/#findComment-1564941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.