Jump to content

loops and select problem


princeads

Recommended Posts

Hi Guys,

 

I've got a problem when trying to fill a select (multples allowed) menu with data from the database. It seems to retrieve the data fine, but I have an inner loop that tries to find any matches with its id to the outer loop and then select the value or not.

 

Any help on this would be much apprieciated.

 

<select id="pageID" multiple="multiple" size="20" name="pageID[]">

<?php

  while ($row4 = mysql_fetch_assoc($result4)){

while ($row5 = mysql_fetch_assoc($result5))

{

            if($row4['ID'] == $row5['pageID'])

{ ?>

                              <option selected="selected" value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option>

                          <?php

}

else{?>

      <option value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option>

<?php }

}

  }?>

</select>

Link to comment
https://forums.phpfreaks.com/topic/91551-loops-and-select-problem/
Share on other sites

I'm assuming this situation.  Where match in the two tables, show selected.

If no match then, because we LEFT JOIN, pageID from table5 will have null value.

 

[pre]

table4                  table5

--------                ---------

ID      ------------<  pageID

title

secondtitle

[/pre]

 

 

<?php
$sql = "SELECT a.ID, a.title, a.secondtitle, b.pageID
        FROM table4 a
        LEFT JOIN table5 b ON a.ID = b.pageID";
        
echo '<select id="pageID" multiple="multiple" size="20" name="pageID[]">';

$res = mysql_query ($sql) or die (mysql_error());

while (list($id, $title, $title2, $page) = mysql_fetch_row($res))
{
    $sel =  $page ? 'selected' : '';
    echo "<option $sel value='$id'>$title $title2</option>";
}
echo '</select>';

Hiya thanks for your help, really is appriciated.

 

I think what your saying is what I'm trying to achieve, to give it more background I have a pluginpages table that shows all the pages and their plugins. There is also a cmspages table to record all the pages on the site. Therefore I'm trying to provide a select menu to allow the user to first see which pages their plugin is on and then secondly change the select menu if they want it to appear on different pages.

 

The code you provided didn't seem to work unfortunately. Here is more of my original code.

 

$query4 = "SELECT * FROM cmspages";

$result4 = mysql_query($query4) or die("ERROR: Select page details problem");

 

$query5 = "SELECT * FROM cmspluginpages WHERE pluginID = '$pluginID'";

echo $query5;

$result5 = mysql_query($query5) or die("ERROR: Select plugin details problem");

 

<select id="pageID" multiple="multiple" size="20" name="pageID[]">

<?php

  while ($row4 = mysql_fetch_assoc($result4)){

  while ($row5 = mysql_fetch_assoc($result5))

  { 

                if($row4['ID'] == $row5['pageID'])

      { ?>

                              <option selected="selected" value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option>

                          <?php

      }

      else{?>

            <option value="<?= $row4['ID'] ?>"><?= $row4['title'] ?> <?= $row4['secondtitle'] ?></option>

      <?php }

  }

  }?>

</select>

 

Am I just being an idoit with not getting your code to work?? Sorry for the trouble, having one of those days! cheers

Adam

 

 

how about

<?php
$sql = "SELECT a.ID, a.title, a.secondtitle, b.pageID
        FROM cmspages a
        LEFT JOIN cmspluginpages b ON a.ID = b.pageID
                AND b.pluginID = '$pluginID' ";
        
echo '<select id="pageID" multiple="multiple" size="20" name="pageID[]">';

$res = mysql_query ($sql) or die (mysql_error());

while (list($id, $title, $title2, $page) = mysql_fetch_row($res))
{
    $sel =  $page ? 'selected' : '';
    echo "<option $sel value='$id'>$title $title2</option>";
}
echo '</select>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.