-1

I'm trying to understand two things:

  1. If the following is possible;
  2. What I am doing wrong with implementing it.

I have an array in PHP which is manually coded:

$q[1] = 'Monday';
$q[2] = 'Tuesaday';
$q[3] = 'Wednesday';

I have a seperate mysqli multi-query that is bringing back another set of data. As I iterate though the multi query and echo results, I need to include information that is within the manually coded PHP array, as detailed above - my current code is as follows:

echo "<tr><td class='qnum'><span class='bold'>". $n .".</span></td>
<td width='450px' > ". $q[$n] ." (<span class='italics'>". $row[0] ."</span>)</td>
<td class='set2'>".$row[9]."%</td>";

I have a pointer $n that increases as the mysqli query loops through the results and I'm using the $q[$n] to try and pull in the relevant data from the PHP query but it doesn't work. However, what is odd is that is I change $q[1] = 'Monday'; to $q = 'Monday'; and then reference $q in my echo statement about - it works!!!

I've also included the mysqli code for reference below:

if ($mysqli->multi_query($query)) {
    $n = 0;

        do {
            if ($result = $mysqli->store_result()) {
        $i = 1;
        $p = 1;

    while ($row = $result->fetch_row()) {
        $n++;

 echo "<tr><td class='qnum'><span class='bold'>". $n .".</span></td>
<td width='450px' > ". $q[$n] ." (<span class='italics'>". $row[0] ."</span>)</td>
<td class='set2'>".$row[9]."%</td>";

                echo "</tr>";
        $result->free();
    }

    } while ($mysqli->next_result());
}

$mysqli->close();

Any thoughts, ideas, suggestions on where I am going wrong?

Homer_J
  • 3,277
  • 12
  • 45
  • 65
  • if this is a proper snippet then you have a major syntax error, there is 2 loops that is not closed and also 2 if conditions that is not closed – DevZer0 Aug 13 '13 at 10:51
  • Hi DevZer0 - it's the top half of the code - it's a huge piece of code, really to long to post here. The loops and conditions are properly closed. – Homer_J Aug 13 '13 at 10:53
  • 1
    well without seeing whats going on the rest of your loop its hard to say what's wrong, can you reproduce the same problem in a reasonable set of lines to post here – DevZer0 Aug 13 '13 at 10:54
  • I've added the bottom section of the code - that should be right. – Homer_J Aug 13 '13 at 10:58
  • In what way doesn't it work? – Jim Aug 13 '13 at 11:03
  • Hi Jim - when using `$q[1] = 'Monday'` and `$q[$n`] where `$n = 1` - I would expect `Monday` to be echoed - it isn't. However, when I simply do `$q ='Monday'` and `echo $q` it works. – Homer_J Aug 13 '13 at 11:06
  • What happens if you `var_dump` `$q` and `$n` when in the loop? – Jim Aug 13 '13 at 11:16
  • $n comes back correctly, getting nothing for the $q. – Homer_J Aug 13 '13 at 11:53

2 Answers2

0

Looks like your do-while loop is a bit odd... shouldn't the syntax be do { ... } while(condition); You seem to be using a mixture of two.

Also, your $q has only three elements... If you're sure you want to use it with $n that increments in a loop, maybe use it with modulo so there's always a $q index for $n?

Example: $q[$n % count($q)]

I'm just fishing here because I'm not entirely sure what's your aiming at.

dan
  • 136
  • 8
  • There are $q[1] to $q[50] - so there is no point posting all of the them! I did three for illustrative purposes. The manual array has 50 and also the mysqli returns 50 as well - they match. Hence the `$q[$n]` code. – Homer_J Aug 13 '13 at 10:59
-1

I didn't ensure that the first PHP manual array was actually correct - rookie error!

$q = array(1 => 'Monday', 'Tuesday', 'Wednesday');
Homer_J
  • 3,277
  • 12
  • 45
  • 65