By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,830 Members | 2,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,830 IT Pros & Developers. It's quick & easy.

Show selected record in different style

P: 4
If I use this code to get records from mysql

Expand|Select|Wrap|Line Numbers
  1.           <ul>
  2.             <?php
  3. $cquery = mysql_query ("Select * From product Where category_id=$category_id");
  4. while ( $cr = mysql_fetch_array ($cquery)) {
  5. while ( $pr = mysql_fetch_array ($cquery) ) {
  6. ?>
  7.             <li>
  8.  
  9.               <a href="index.php?product=<?php echo $category_id; ?>&product_id=<?php echo $pr['product_id']; ?>"><?php echo $pr['name']; ?></a>
  10.  
  11.  
  12.             </li>
  13.             <?php  } } ?>
  14.           </ul>
Then how can I apply different style to the clicked product, like if below code results:

product 1
product 2
product 3

And then if some one click on product 1, it should be like this:

product 1
product 2
product 3

I have tried with several things but have not succeeded yet. What is the best way to do this?

Thanks.
Jan 4 '12 #1
Share this Question
Share on Google+
7 Replies


Rabbit
Expert Mod 10K+
P: 12,366
I would put each product in it's own container, probably a div, and use the on click event to run a javascript function that changes the class. Don't forget to change the class of the previously selected item back.
Jan 4 '12 #2

P: 4
Thanks, but isn't there a way for it be done with if comparison,

like if (selected) then <a class="active"> ???
Jan 4 '12 #3

Rabbit
Expert Mod 10K+
P: 12,366
It sounds like you're trying to do it using only PHP but that's impossible because the PHP engine has no idea what elements the user clicks on. That has to be handled on the client side using javascript.
Jan 4 '12 #4

P: 4
great, and can I have such example with JS?
Jan 4 '12 #5

Rabbit
Expert Mod 10K+
P: 12,366
Give it a shot and then post what you have. We can work through the errors you run into.
Jan 4 '12 #6

P: 4
Thanks but can you provide me some link where such usage of JS is described because I am a beginner to PHP. :(
Jan 4 '12 #7

Rabbit
Expert Mod 10K+
P: 12,366
Javascript is unrelated to PHP. You'll want to start out with a tutorial on Javascript. You can try the one on the w3schools website.
Jan 4 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.