News:

Join the Facebook Fan Page.

Main Menu

MySQL & PHP

Started by Luis-, April 19, 2012, 04:11:19 PM

Previous topic - Next topic

Luis-

Hi, i'm making a User Control Panel for a project of mine, I am wondering how I could make a selection list with colours in them but the colours which are being used already wont show. When someone gets a colour it's saved into a MySQL row like this "FFFFFF", how can I check to see if that colour has already been taken?

Colin

I can help you write something in PHP for this. Is this an addon for SMF or just a completely separate script?
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Luis-

It's a seperate script.

MrPhil

So you have a list (array) of possible colors (let's call it $colors) and a database table which includes used_color for each user. You want to build a selection list of all the colors in $colors that are not found in used_color? I would start by building an array of all used colors, taken by reading used_color in the table. Call it $used_colors. You're going to have to loop through the $colors list one by one anyway to build each <option> tag, so take that opportunity to check if that $colors[$i] element is found in the $used_colors array:
...output <select> tag...
for ($i=0; $i<count($colors); $i++) {
   if (!in_array($colors[$i], $used_colors)) {
      ... output <option> tag for selection list with $colors[$i] and the associated name ...
   }
}
... output </select> tag ...

Luis-

So, i'd query the table row which stores the colours and do something like this;

$row['Colour'] = array();

and then loop it?

MrPhil

No, it would be more like
$used_colours = array();
$results = mysql_query("SELECT colour FROM whatever table");
loop until $results exhausted, putting into $row array
  $used_colours[] = $row['colour'];  // assumes used colours are unique
end loop

There are plenty of places in SMF to copy the code from and adjust to your needs. Then once you have the $used_colors array, use the code from my previous post, which assumes you built an array of $colours at some point.

That's just one way of doing it. If you ask half a dozen programmers how to do it, you'll get back as many as six somewhat different methods.

Luis-

Here's my code so far, it's still not working and I have no idea since I haven't worked with arrays before.

<?php
$colors 
= array(
"FFFFFF",
"00FFFF",
"B8860B"
);

$used_colours = array();
$results mysql_query("SELECT Colour FROM airlines ORDER BY `airlines`.`ID` ASC LIMIT 0, 30");
while(
$row mysql_fetch_assoc($results)) {
  
$used_colours[] = $row['Colour'];
}
?>
<select size="1"> <?php
for ($i=0$i<count($colors); $i++) {
   if (!
in_array($colors[$i], $used_colors)) {
      
?>
<option value=<?php echo $colors[$i]; ?>"><?php echo $colors[$i]; ?></option><?php
   
}
}
?>
</select>

Colin

Are you getting some sort of error?
"If everybody is thinking alike, then somebody is not thinking." - Gen. George S. Patton Jr.

Colin

Luis-

No, it's just showing all of the colours in the $colors array but one of the users in my table is already using it, yet it's still showing.

MrPhil

You've got to decide on a consistent spelling for $colors/$colours and $used_colors/$used_colours.

Advertisement: