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
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>';

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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>';

Link to comment
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.