Using Images instead of number

Discussion in 'PHP' started by CuBz, Jul 13, 2009.

  1. #1
    I was wondering how i can use images instead of numbers in php

    What i mean is, i have a code that shows how many people are online but it shows the number of people in text, i want it to show it in pictures of numbers instead of text numbers if you know what i mean, i cannto explain it any easier

    here is the code:

    
    <?php
    $timenow=time();
    
    $select = mysql_query("SELECT * FROM users WHERE online > '$timenow' ORDER by id");
    
    $num = mysql_num_rows($select);
    
    echo "$num";
    ?> Online Users
    
    PHP:
    I want it to show the numbers as images, i will obciously provide the images myself

    Thanks
     
    CuBz, Jul 13, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    How about:

    
    if($num < 10){
    echo "<img src=\"$num.jpg\" />";
    }
    else{
    $num = str_split($num);
    echo "<img src=\"$num[0].jpg\" /><img src=\"$num[1].jpg\" />";
    
    PHP:
    Then save the images as the number ?

    And obviously you could get the amount of numbers if you have hundereds or thousands online with sizeof function, or just extend the if to else if.
     
    wd_2k6, Jul 13, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    I would suggest using css and styling the text to be something that looks like what you want. It will be much faster and you wont have to deal with real images.

    echo '<span class="user_count">'.$num.'</span>';

    If you still want to use images, the easiest way would be to create a bunch of number images, and just replace the image url with the count.

    EX:

    $num = mysql_num_rows($select);

    echo '<img src="/images/count_'.$num.'.jpg" />';

    You could also use GD to generate images on the fly, but this would be way overkill and a waste of resources in this situation.
     
    jestep, Jul 13, 2009 IP
  4. CuBz

    CuBz Peon

    Messages:
    117
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What do i put incase i have between 100 - 999 users online?
     
    CuBz, Jul 13, 2009 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could either do:

    
    if($num < 10){
    echo "<img src=\"$num.jpg\" />";
    }
    else{
    $num = str_split($num);
    //Loop through each number
    foreach($num as $n){
    echo "<img src=\"$n.jpg\" />";
    }
    
    PHP:
    or i read the other day someone was saying a for loop was less maintanence than a for-each loop:

    
    if($num < 10){
    echo "<img src=\"$num.jpg\" />";
    }
    else{
    $num = str_split($num);
    //Loop through each number
    for($x = 0; $x <= sizeof($num); $x++){
    echo "<img src=\"$num[$x].jpg\" />";
    }
    
    PHP:
    For the above 2 examples, you wouldn't even need the if else statement, as it would work for single digit numbers aslo, but i left it in there just incase you often have less than 10 users online then you wouldn't need to use the str_split function every time.

    ontop of this (i am spoiliing you) you could have a third option and add another else clause:
    
    if($num < 10){
    echo "<img src=\"$num.jpg\" />";
    }
    elseif($num < 100){
    //2-digit num
    $num = str_split($num);
    echo "<img src=\"$num[0].jpg\" /><img src=\"$num[1].jpg\" />";
    }
    else{
    //Then theres a 3 digit number
    $num = str_split($num);
    echo "<img src=\"$num[0].jpg\" /><img src=\"$num[1].jpg\" /><img src=\"$num[2].jpg\" />";
    }
    
    PHP:
     
    wd_2k6, Jul 13, 2009 IP