Note that there are some explanatory texts on larger screens.

plurals
  1. POWhile Loop Seems Endless
    primarykey
    data
    text
    <p>I have a table named <code>getinvolved</code> and in this table there are various fields and values, obviously. One such is named <code>The Route</code> and contains text separated by commas. For the sake of this we'll say they are <code>Spot 1, Spot 2, Spot</code>. Yes the spaces matter.</p> <p>Now, in my code I first break this value up into an array based on the commas: <code>$route = explode(",", $row['Content']);</code> Content is the name of the row that contains the <code>Spot 1, Spot 2, Spot</code> text value. This all works fine, I have echo'd this and it appears perfectly.</p> <p>Next comes the tricky bit, I have multiple other entries that are instead of being labelled <code>The Route</code> are instead labelled <code>Route</code>. This is an entry for each possible spot, while the current route is <code>Spot 1, Spot 2, Spot</code> it could always change to <code>Spot 2, Spot 1, Tops</code>.</p> <p>Each possible entry has it's own row: <code>Name, Type, Content</code>. <code>Type</code> is where <code>Route</code> is set, <code>Name</code> is the name of the spot, and <code>Content</code> contains a Google Maps URL to show the spot in Google Maps Street View.</p> <p>What I'm trying to do is take my array <code>$route[]</code> which currently contains <code>Spot 1, Spot 2, Spot</code> and check those values against all others. I've created a separate array <code>$echoroute[]</code> and this contains the actual route with all the information.</p> <p>My code is as follows:</p> <pre><code>$sql = mysqli_query($link, "SELECT * FROM getinvolved WHERE Type='Route'"); $echoroute = array_fill(0,count($route),""); #Gives $echoroute[] 3 empty elements as $route will always contain 3 elements at the moment. $i = 0; $max = count($route); if($sql) { foreach($route as $ii) { while($row = mysqli_fetch_assoc($sql)) { if($row['Name'] == $ii) { $echoroute[$i] = "&lt;a href='" . $row['Content'] . "' target='_blank'&gt;" . $row['Name'] . "&lt;/a&gt; --&gt; "; echo $i . " / " . $max . "&lt;br /&gt;"; $i+=1; if($i==($max)) {break 2;} else {break 1;} } } } } </code></pre> <p>I'm unable to figure out why but at present during running this code all that happens is it will go through the <code>if</code> as <code>$sql</code> passes, it enters the first and second <code>while</code> loops and then gets to the <code>if($row['Name'] == $route[$i])</code> part. Here it seems to work fine until it actually enters the <code>if</code> statement (the value in <code>$row['Name']</code> is equal to the one in <code>$route[$i]</code>).</p> <p><code>$total</code> is increased as where I ask it to echo it does, but only once. The code ends and displays the following:</p> <pre><code>0 / 3 3 </code></pre> <ul> <li>The <code>1</code> represents the <code>$total</code> variable, which should go through 3 times according to <code>while($total&lt;$max)</code>.</li> <li>The <code>/</code> is me adding a separator for ease of reading.</li> <li>The <code>3</code> is the <code>$max</code> variable, or <code>count($route)</code>, the total number of spots included in the <code>$route</code> array.</li> <li>And finally the trailing <code>3</code> is a <code>count()</code> function of <code>$echoroute</code> letting me know there are 3 elements in that array.</li> </ul> <p>Ultimately, my question is, why does <code>$total</code> never get to 2 and finish the loop as I would require?</p> <p>This took a while to write, so I hope you understand I've put a bit of thought into this, I wrote this a while ago, and it needs some updating and this is where I am currently at.</p> <p>EDIT:<br> Have narrowed this down now! Using several echo functions, it appears that the last run through starts, but only makes it as far as the while statement. It never actually enters the while statement. Currently I have two results echo'd and the last result just so happens to be at the bottom of the table. It's the last one to be used before the while($row = statement ends of it's own accord. Am I trapping the while statement or do I need to release it or something? I'm really confused and so close to having the final piece!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload