is it possible to avoid the last loop??
Of course, but I think that there are problems more important than that that need to be addressed.
$query = (" SELECT * FROM grids_winning ORDER BY id_grids DESC LIMIT 1 ");
$run_query = mysql_query($query) or die ('Invalid query: ' . mysql_error());
$result = mysql_num_rows($run_query);
$i = 0;
while ($i < $result)
{
//retrieve the latest winning grid
$draw = mysql_result($run_query,$i,'grid_winning');
$i++;
}
Is better expressed as:
//retrieve the latest winning grid
$query = "SELECT grid_winning FROM grids_winning ORDER BY id_grids DESC LIMIT 1";
$result = mysql_query($query) or die ('Invalid query: ' . mysql_error());
if (mysql_num_rows($result) == 1)
{
$draw = $row['grid_winning'];
$array = strtoArray($draw);
// ...
There is no need for a loop since the query only returns at most one row.
Next: in database schema design, we generally try to avoid having fields with compound values. $draw gets its value from a single field, yet it can be converted into an array. This is typically a sign of a poor database schema, and is why you end up having another for loop which runs a query on each iteration.
With a different database schema, it may even be possible to use just one query to get all the results that you want. I suggest reading the PHPBuilder article on Database Normalization and Design Techniques for an introduction of what can be done.