Jump to content

Recommended Posts

Hi there, im trying to nest two do loops. The first loop works fine but the last loop doesnt seem to work at all.

 

 

 

<? $colname_Planet = "-1";
if (isset($_GET['recordID'])) {
  $colname_Planet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_swb, $swb);
$query_Planet = sprintf("SELECT * FROM planet WHERE PlanetName = %s", GetSQLValueString($colname_Planet, "text"));
$Planet = mysql_query($query_Planet, $swb) or die(mysql_error());
$row_Planet = mysql_fetch_assoc($Planet);
$totalRows_Planet = mysql_num_rows($Planet);

$plans = $row_Planet['PlanetName'];

$colname_Fleet = "-1";
if (isset($_GET['recordID'])) {
  $colname_Fleet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_swb, $swb);
$query_Fleet = sprintf("SELECT * FROM fleet WHERE PlanetName =  '$plans'");
$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());
$row_Fleet = mysql_fetch_assoc($Fleet);
$totalRows_Fleet = mysql_num_rows($Fleet);

$fleetships = $row_Fleet['FleetName'];

$colname_Ships = "-1";
if (isset($_GET['recordID'])) {
  $colname_Ships = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_swb, $swb);
$query_Ships = sprintf("SELECT * FROM ships WHERE FleetName = '$fleetships'");
$Ships = mysql_query($query_Ships, $swb) or die(mysql_error());
$row_Ships = mysql_fetch_assoc($Ships);
$totalRows_Ships = mysql_num_rows($Ships); ?>
<?php do { ?>
<?php echo $row_Fleet['FleetName']; ?>
<?php echo $row_Fleet['PlanetName']; ?>
<?php do { ?>
<?php echo $row_Ships['ShipName']; ?>
<?php } while ($row_Ships = mysql_fetch_assoc($Ships)); ?>
<?php } while ($row_Fleet = mysql_fetch_assoc($Fleet)); ?>

 

If you can spot anything wrong, please let me know.

 

Thanks :)

 

Link to comment
https://forums.phpfreaks.com/topic/234551-nested-do-loop/
Share on other sites

That do while and every line with it's separate php tags is quite confusing. Why don't you just write it as follows:

 

<?php
while ($row_Fleet = mysql_fetch_assoc($Fleet)) {
     echo $row_Fleet['FleetName'];
     echo $row_Fleet['PlanetName'];
     
     while ($row_Ships = mysql_fetch_assoc($Ships)) {
          echo $row_Ships['ShipName'];
     }
}
?>

 

while() and do while() won't make any difference in your case, but the code is much more readable.

 

As for the problem with the second while, I'm not sure as it looks fine. Are you getting any errors in the mysql_query() parts? Can you loop individually the results? Try doing some debugging if data is returned correctly.

 

EDIT: By the way, from what I can see, you can get by with a single query (and a single loop) by using a JOIN. I suggested the use of JOINs to someone else in another topic which you can find here. That will probably help.

Link to comment
https://forums.phpfreaks.com/topic/234551-nested-do-loop/#findComment-1205411
Share on other sites

You will be far better of learning joins. Joins will allow you to get data from more than one table within a single query (as long as your data relates to each other). Having to manage three separate loops to piece together the data is not worth doing and just makes things much harder.

 

The join I provided earlier should work with your ships table too, Example query

SELECT p.PlanetName, p.PlayerName, f.FleetName, f.Status, s.ShipName FROM Planets p
LEFT JOIN Fleet f ON (p.PlanetName = f.PlanetName)
LEFT JOIN Ships s ON (f.FleetName = s.FleetName)
WHERE p.PlanetName = '$colname_Planet'

Link to comment
https://forums.phpfreaks.com/topic/234551-nested-do-loop/#findComment-1205581
Share on other sites

There are no mysql errors at all Theres just nothing outputted for the Ship query. It has been working indepenantly before but not since i tried to nest the loops and use the fleet query as well.

 

Is there anything wrong with the logic because... the Fleet records are selected where the PlanetName is = PlanetName of the Planet table and the Ships are selected where the Fleetname = FleetName of the Fleets table.

 

The php do displays all the fleets records that are on the planet in question and the other one is suppose to display all of the ships in those fleets.

 

Thanks for the help ill try it out with the join... as it makes better coding but id really like to try and find out what was wrong this way.

 

:)

 

Link to comment
https://forums.phpfreaks.com/topic/234551-nested-do-loop/#findComment-1205589
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.