Just learning - php on simple lists

Discussion in 'PHP' started by mnymkr, May 4, 2007.

  1. #1
    Hi, man I feel dumb asking this after reading some of the other posts but I am learning. Hell I learned quantum mechanics I can learn this right.

    Here is my simpleton code and I have two questions:

    How can I make every odd name in the list a different color?

    How can I make links to sort this by first name and last name?

    Please not too complex as I am trying to get the logic down first. Thanks!

    <?php
    	//Open a connection to the mysql server
    
    
    	$link=mysql_connect("localhost","bjraines_1","bs624p15");
    	if(!$link) {
    		print("Failed to establish connection to mysql server!");
    		exit();
    	}
    	//Select the database
    	$status=mysql_select_db("bjraines_php");
    
    
    //Run query
    	$query="SELECT first, last FROM list ";
    	$rs=mysql_query($query);
    	if(!$rs) {
    		print("Query Error: ".mysql_error());
    	}
    	$numrows=mysql_num_rows($rs);
    	print("Number of rows returned: $numrows <br />
    ");
    
    
    
    
    
    if (!$rs) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    if (mysql_num_rows($rs) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }
    
    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    
    while ($row = mysql_fetch_assoc($rs)) {
    
    echo "<ul>";
        echo "<li>".$row["userid"].$row["first"].$row["last"]."</li>";
       echo "</ul>"; }
        ?>
    Code (markup):

     
    mnymkr, May 4, 2007 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Ordering is easy :)

    
    <?
    // get order from URL and stip any strings that may lead
    // to SQL injections
    $order = mysql_escape_string($_GET['order']);
    
    // Using this method you will call up your links like:
    // http://www.yoursite.com/script.php?order=first
    // or http://www.yoursite.com/script.php?order=last
    
    if($order == 'first'){
         // Order By First
         $sql = mysql_query("select first,last from list order by first") or die(mysql_error());
    }
    
    else if($order == 'last'){
         // Order By Last
         $sql = mysql_query("select first,last from list order by last") or
         die(mysql_error());
    }
    
    else{
        // no order specified, or user altered order field in URL
        $sql = mysql_query("select first,last from list") or die(mysql_error());
    }
    
    ?>
    
    PHP:
    You will need to call up the respective SQL query, or appropriate URL when you are trying to sort the information.

    Now, getting the lists to have different alternating background colors... This is straight off the top of my head so you might have to work out some errors but i'll explain it as I go :)

    
    <?
    
    // get rows
    $sql = mysql_query("select * from list") or die(mysql_error());
    
    // get all rows from the database
    while($row = mysql_fetch_assoc($sql)){
      // I assume you have a primary key, such as ID in your table
      // or some equivalent?
    
      // set $id equal to row id
      $id = $row['id'];
    
      // create full name from database
      $name = $row['first'] . ' ' . $row['last'];
    
      // Get background: Basically the If Statement just says that
      // if $id divided by 2 has a remainder, then use background color
      // #CCCCCC, else use #F2F2F2. And as we all know, odd numbered
      // ID's will result in a remainder and evens will not. Therefore the colors
      // selected will alternate
    
      if($id % 2)
         $bg = '#CCCCCC';
      else
         $bg = '#F2F2F2';
    
      print '<div style="background: ' . $bg . '; padding: 3px;">' . $name . '</div>';
    }
    
    ?>
    
    PHP:
    Or something like that. Basically, in your while loop (or For loop, you can use either) check if your primary key is an even or odd number (based on if it leaves a remainder or not). If it does leave a remainder use the first color (it's an odd number) else use the second color (it's an even number :) )

    It's late so there might be a few errors, but that should get you on track :p

    Hope that helps!
     
    Louis11, May 4, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Hey thanks alot! That makes a lot of sense and helps me with another project I am working on!
     
    mnymkr, May 5, 2007 IP
  4. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #4
    just a quick question

    i am not sure i understand this statement....can i use it if I ma not pulling from a database

    mysql_escape_string


    could i use this instead?

    addslashes
     
    mnymkr, May 5, 2007 IP