R
R
Rostislav2014-09-28 17:51:34
PHP
Rostislav, 2014-09-28 17:51:34

How in PHP write a for() loop to an array and output its variable?

With this I pull data from mysql

$result=mysql_query("SELECT id,address,phone,delivery_id FROM s_orders WHERE status = '0' AND paid = '1' AND delivery_id != '5' ORDER BY phone");

With this I can display data on the screen
echo "<table border=1>";
 for($i=0;$i<$n;$i++) echo "<tr><td>",mysql_result($result,$i,"id"),"</td><td>",mysql_result($result,$i,"address"),"</td><td>",mysql_result($result,$i,"phone"),"</td></tr>";
 echo "</table>";
 mysql_close($db);

How to add all this data either to an array or to a variable to be used in another operation, specifically using the SMS sending api
Example:
$addrbook_id = "2321321";
$res = $Stat->createCampaign("testName",
            "Адреса заказов: $fst",
            $addrbook_id, "", 0, 0, 0, "");

How to output a loop from the data or an array instead of the $fst variable
Thank you

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex Chistyakov, 2014-09-28
@postislav

It should be something like this:

$rows=array();
$result=mysql_query("SELECT id,address,phone,delivery_id FROM s_orders WHERE status = '0' AND paid = '1' AND delivery_id != '5' ORDER BY phone");
for($i=0;$i<$n;$i++) { 
   $row=array("id" => mysql_result($result,$i,"id"),
          "address" => mysql_result($result,$i,"address"),
          "phone" => mysql_result($result,$i,"phone")
        );
   array_push($rows, $row);
}
mysql_close($db);

$rows will contain an array of associative arrays of results
$rows[0] - the first result, $rows[0]["id"] - the id of the first result
New version:
$addrs=array();
$result=mysql_query("SELECT id,address,phone,delivery_id FROM s_orders WHERE status = '0' AND paid = '1' AND delivery_id != '5' ORDER BY phone");
for($i=0;$i<$n;$i++) { 
   array_push($addrs, mysql_result($result,$i,"address"));
}
mysql_close($db);

Now here's what you can do:
$Stat->createCampaign("testName",
"Адреса заказов: $addrs",
$addrbook_id, "", 0, 0, 0, "");

If there the function expected an array of addresses, then here it is, an array of addresses.
Another addition:
$Stat->createCampaign("testName",
"Адреса заказов: " . implode(',', $addrs),
$addrbook_id, "", 0, 0, 0, "");

Now this function that was expecting a string will get a string instead of an array

S
Sergey, 2014-09-28
Protko @Fesor

Well, I'll start with the fact that mysql_query is deprecated, that is, for God's sake, don't use this extension. Only mysqli or pdo.
Well, I’ll end up with the fact that you first need to collect the data into an array (there are guidelines in the documentation) and then do anything with this array. For example:
$addresses = array_column($rows, 'address');

K
KorsaR-ZN, 2014-09-28
@KorsaR-ZN

If you are using mysql_*, which is no longer supported and considered unsafe.
i.e. special function - mysql_fetch_assoc

$result = mysql_query("SELECT id,address,phone,delivery_id FROM s_orders WHERE status = '0' AND paid = '1' AND delivery_id != '5' ORDER BY phone");

if ($result) {
   while ($row = mysql_fetch_assoc($result)) {
      // Обработка строк...
   }
}

In general, switch to PDO

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question