Question

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?

Était-ce utile?

La solution 2

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

$q = array(1 => 'Monday', 'Tuesday', 'Wednesday');

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top