Jump to content

change color of results


web_master

Recommended Posts

Hi,

I got a php script (downloaded from some site) and this is the part of scripts to autocomplete results. The result give to me all results in same color. I want to some results to see in other colors. In table I have a row "onoff", where have a 1 (on) and 0 (off) So I want to listed results with 0 (off) to see in red color.

 

here is the php code:

 

$result = mysql_query("SELECT `zeneszamok_zeneszam`, `zeneszamok_onoff` FROM `zeneszamok` ");
while ($row = mysql_fetch_assoc($result)) {
$finals[]=$row['zeneszamok_zeneszam'];

}
mysql_free_result($result);
mysql_close($link);


// check the parameter
if(isset($_GET['part']) and $_GET['part'] != '')
{
// initialize the results array
$results = array();

// search colors
foreach($finals as $final)
{

// if it starts with 'part' add to results
if( strpos($final, $_GET['part']) === 0 ){
$results[] = $final;
}
}


// return the array as json with PHP 5.2
echo json_encode($results);
}

 

 

 

 

Thanks in advanced

Link to comment
Share on other sites

Since you didn't post your client-side code showing how you are using the JSON data, it's not directly possible to help, but in general, you would apply a css class to the output having the onoff column set to a zero.

 

You could do that in the server-side code, by putting the values inside a span tag with the correct css class name or you could do it in the client-side code, by passing the onoff value with each corresponding value in the JSON data, and then setting the className of the element that you output the value into.

 

A couple of points about your code -

 

1) There's no need to repeat the database table name as part of each column name. That results in more typing, typo's, and longer query statements.I would name the columns - zeneszam and onoff.

 

2) Your search filter in $_GET['part'] should be in your query. In fact, retrieving all the rows from your table first, then looping over them two times, the second time to find values using php code is probably 8-10 times slower than doing this in the query.

 

You could write your posted code like this -

 

$part = isset($_GET['part']) ? trim(($_GET['part']) : '';
if($part != ''){
   $query = "SELECT zeneszam, onoff FROM zeneszamok WHERE zeneszam LIKE '".mysql_real_escape_string($part)."%'";
   $result = mysql_query($query);
   $results = array();
   while($row = mysql_fetch_assoc($result)){
       $results[]=$row; // pass both the zeneszam and onoff values as an array for the client-side code to use
   }
   echo json_encode($results);
}

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.