Show selected record in different style

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ScottEdward
    New Member
    • Jan 2012
    • 4

    Show selected record in different style

    If I use this code to get records from mysql

    Code:
              <ul>
                <?php
    $cquery = mysql_query ("Select * From product Where category_id=$category_id");
    while ( $cr = mysql_fetch_array ($cquery)) {
    while ( $pr = mysql_fetch_array ($cquery) ) {
    ?>
                <li>
    
                  <a href="index.php?product=<?php echo $category_id; ?>&product_id=<?php echo $pr['product_id']; ?>"><?php echo $pr['name']; ?></a>
    
    
                </li>
                <?php  } } ?>
              </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.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • ScottEdward
      New Member
      • Jan 2012
      • 4

      #3
      Thanks, but isn't there a way for it be done with if comparison,

      like if (selected) then <a class="active"> ???

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • ScottEdward
          New Member
          • Jan 2012
          • 4

          #5
          great, and can I have such example with JS?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Give it a shot and then post what you have. We can work through the errors you run into.

            Comment

            • ScottEdward
              New Member
              • Jan 2012
              • 4

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

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                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.

                Comment

                Working...