Multiple Highlighted List

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Multiple Highlighted List

Post by theoph »

I'm having problems getting php to highlight multiple items that have been inserted into a MySQL field. It highlights fine with one entry, but not with more than one. The multiple items are inserted into the field they are seperated by comas.

Any suggestions?

Code: Select all

<select name="s_cells&#1111;]" size="4" multiple id="select">
    <option value="" <?php if (!(strcmp("", $row_rsLaos&#1111;'s_cell']))) &#123;echo "SELECTED";&#125; ?>>-None-</option>
    <?php
do &#123;  
?>
    <option value="<?php echo $row_rsCellmenu&#1111;'name']?>"<?php if (!(strcmp($row_rsCellmenu&#1111;'name'], $row_rsLaos&#1111;'s_cell']))) &#123;echo "SELECTED";&#125; ?>><?php echo $row_rsCellmenu&#1111;'name']?></option>
    <?php
&#125; while ($row_rsCellmenu = mysql_fetch_assoc($rsCellmenu));
  $rows = mysql_num_rows($rsCellmenu);
  if($rows > 0) &#123;
      mysql_data_seek($rsCellmenu, 0);
	  $row_rsCellmenu = mysql_fetch_assoc($rsCellmenu);
  &#125;
?>
        </select>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Hope you'll understand my point of idea.
When I did this, I used the similarity of the code below:

Code: Select all

while ($row_rsCellmenu = mysql_fetch_assoc($query_result)) {
    $check = (strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']) > 0 ' selected' : '');
    echo '<option '.$check.'name="something" value="'.$row_rsCellmenu['name'].'">'.$row_rsCellmenu['name'].'</option>';
}
Tried to rewrite it some to fit your variables. Rewriting it needed.

Here I use the shorter version of if-then-else, assigning $check with either ' SELECTED' or ''.
As I'm using this loop to print the options with the $check embedded, it will automaticly get selected.

Just If it's an option to rewrite it (or someone else doesn't give another answer/example).
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Thanks

I give it a try.

-Dave
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

JAM wrote:

Code: Select all

while ($row_rsCellmenu = mysql_fetch_assoc($query_result)) {
Got a parse error on the above line. Did we need a ";" after the last ")" and before "{"?

When I inserted the ";", I got a parse error on the second line of code.

Code: Select all

<?php
$check = (strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']) > 0 'selected':''); 

?>
-Dave
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

No, the usage in the example is correct...

But I meant that you would need to rewrite the code snippet i provided to fit your own. It will not work 'as-is'.

I rewrote it to more look like you own. Some modifications might still be needed.

Code: Select all

<select name="s_cells[]" size="4" multiple id="select"> 
    <option value="" <?php if (!(strcmp("", $row_rsLaos['s_cell']))) {echo "SELECTED";} ?>>-None-</option> 
    <?php 
do {  
    $check = (strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']) > 0 ? ' SELECTED' : '');
    echo '<option '.$check.'value="'.$row_rsCellmenu['name'].'">'.$row_rsCellmenu['name'].'</option>';
    <?php 
} while ($row_rsCellmenu = mysql_fetch_assoc($rsCellmenu)); 
?> 
        </select>
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Still getting a parse error on the row that has the following script:

Code: Select all

$check = (strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']) > 0 ? ' SELECTED' : ''); ?>
BTW - Used the whole script you recommended to me.

-Dave
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There's a parenthsis missing:

Code: Select all

(strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell'])
should be

Code: Select all

(strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']))
Mac
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Still getting a parse error on the following line of code:

Code: Select all

$check = (strcmp ($row_rsCellmenu['name'], $row_rsLaos['s_cell']))> 0 ?'SELECTED':'';
Any suggestions?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Oops, my bad, I didn't copy all the code - it should be:

Code: Select all

$check = (strcmp ($row_rsCellmenu['name'], $row_rsLaos['s_cell']) > 0) ? 'SELECTED' : '';
Mac
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

twigletmac wrote:There's a parenthsis missing:

Code: Select all

(strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell'])
should be

Code: Select all

(strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']))
Mac
that's what i LOVE about (x)emacs. the parenthesis matching is a lifesaver at times... specially when using scheme or lisp
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

All parse errors have been fixed and the page now displays with no problems. However, no items are being highted in the Multiple List Box. Even when there is only one item to be hightlighted. The following is the code Dreamweaver MX 2004 generates. It highlights find with one item but not with there is multiple items in a field seperated with comas.

<?php
do {
?>
<option value="<?php echo $row_rsCellmenu['name']?>"<?php if (!(strcmp($row_rsCellmenu['name'], $row_rsLaos['s_cell']))) {echo "SELECTED";} ?>><?php echo $row_rsCellmenu['name']?></option>

<?php
} while ($row_rsCellmenu = mysql_fetch_assoc($rsCellmenu));
$rows = mysql_num_rows($rsCellmenu);
if($rows > 0) {
mysql_data_seek($rsCellmenu, 0);
$row_rsCellmenu = mysql_fetch_assoc($rsCellmenu);
}
?>

Any thing here that would help us getting multiple items in a List Box to highlight?

-Dave
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Workable Code

Post by theoph »

This is the code that finially worked for me. Thanks to all that helped and pointed me into the right direction.

Code: Select all

<?php 
			do {
				if(strpos($row_rsLaos['s_cell'],$row_rsCellmenu['name'])!==FALSE)
				{
					$check = 'SELECTED';
				}
				else
				{
					$check = '';
				}
				echo '<option value="'.$row_rsCellmenu['name'].'" '.$check.'>'.$row_rsCellmenu['name'].'</option>';
			}
			while ($row_rsCellmenu = mysql_fetch_assoc($rsCellmenu));
?>
Post Reply