Need help with increasing a number from 1-5 with PHP

Discussion in 'PHP' started by Neo_The_One, Dec 27, 2007.

  1. #1
    Hi, Guys.

    I`m in a small pickle.. :/


    $i = 1;

    <div id="featuredb-<?=$i?>" class="featuredbox">

    I need it so that the "i" changes value, start from 1 to 5. So that the first output shows 1, the 2nd to show: 2, and so on..

    It does not have to be named "i" or any thing like that i just tryed to add it in my self but failed.

    I allso need the output to be limited at: 5 items.

    I need to make so that i can get an arrow that i can output few places in the php / html code to get the right class / ID for styling it etc.

    Any one?


    Here is the PHP CODE:

    function display_todaysmostpopular($base_url,$rewrite,$sedir){
    
    ?>
    
    <?
    $x = 0;
    $sql_query = "SELECT * from games WHERE gamestatus = 1 ORDER BY playedtoday DESC LIMIT 24";
    //store the SQL query in the result variable
    $result = mysql_query($sql_query);
    if(mysql_num_rows($result))
    {
    //output as long as there are still available fields
    while($row = mysql_fetch_array($result))
    {
    $x++;
    $i = 1;
    $mostpopulargametitle = $row[gametitle];
    $mostpopulargametitle = stripslashes($mostpopulargametitle);
    $mostpopulargameid = $row[gameid];
    $mostpopulartimesplayed = $row[timesplayed];
    $gamedescription = $row[$gamedesc];
    $sepopular = str_replace (" ","-",$mostpopulargametitle);
    $sepopular= str_replace ("'","_",$sepopular);
    $mostpopularicon = $row[gameicon];
    $iconlocation = $row[iconlocation];
    $mostplayed = $row[playedtoday];
    
    // If SE Friendly is turned OFF, this will be displayed
    if ($rewrite != 1)
    $gameurl = $base_url."index.php?action=playgame&gameid=".$mostpopulargameid;
    
    else
    $gameurl = $base_url.$sedir."/".$sepopular;
    
    if ($iconlocation ==1)
    $gameicon = $mostpopularicon;
    else
    $gameicon = $base_url."games/images/".$mostpopularicon;
    
    ?>
    <?if ($x ==1 || $x == 9) echo "";?>
    <div id="featuredb-<?=$i?>" class="featuredbox">
                                <a href="<?=$gameurl;?>" title="Click to play <?=$mostpopulargametitle;?>" class="featured-1"><img  src="<?=$gameicon;?>" width="180" height="130" alt="" /></a>
                                <h3><?=$mostpopulargametitle;?></h3>
                                <p>
                                    <?=$gamedescription;?>
                                    <br><?=$mostplayed;?> plays
                                </p>
                                <a href="#" class="playnow">Play Now! &raquo;</a>
                            </div>
    <?if ($x ==8 || $x == 26) echo "";?>
    <?
    
    
    }
    }
    
    ?>
    
    <?
    }
    
    
    ?>
    
    PHP:
     
    Neo_The_One, Dec 27, 2007 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure where that would go as I didn't read the whole code but :
    
    for ($i=1; $i<=5; $i++)
    {
    //put code here
    }
    
    Code (markup):
     
    maiahost, Dec 27, 2007 IP
  3. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Hi,

    Well if i put the

    for ($i=1; $i<=5; $i++)

    in the start of the code where the $i is now, how can i echo that number to the output field?


    PS: Thank you for your help! :)
     
    Neo_The_One, Dec 27, 2007 IP
  4. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    just echo $i and it will be 1, 2, 3, 4 and then 5 every time the query executes (every row)
     
    maiahost, Dec 27, 2007 IP
  5. jronmo

    jronmo Guest

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Take the $i =1 out of your while loop. It resets itself to 1 every time you do the loop.
    Use $i++ inside the loop to increment. Its like saying $i +1
     
    jronmo, Dec 27, 2007 IP
  6. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Hi,

    How would you implant that $i++ into the code above?

    And thanks again, i will have more issues with PHP small fixes. If someone wants to take the job, please post your payment request and it will be done! :)

    Thanks
     
    Neo_The_One, Dec 28, 2007 IP
  7. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is pretty simple

    same as the $x.

    i would feel like i was taking money for free accepting this job:)

    as for the limit : you mean the results if so change LIMIT in the query to 0,5
     
    coches, Dec 28, 2007 IP
  8. jronmo

    jronmo Guest

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Neo,
    I believe you could simplify your problem by modifying your query to only return the TOP 5 games of the day instead of using PHP logic. This would depend on how your database is organized, and how you log the number of times a game has been played. You can PM me if you want to discuss.
     
    jronmo, Dec 28, 2007 IP
  9. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #9
    here is the code i used:

    <?php for ($i=1; $i<=5; $i++)
    {
    if ($x ==1 || $x == 9) echo "";?>
    <div id="featuredb-<?=$i?>" class="featuredbox">
    <a href="<?=$gameurl;?>" title="Click to play <?=$mostpopulargametitle;?>" class="featured-1"><img src="<?=$gameicon;?>" width="180" height="130" alt="" /></a>
    <h3><?=$mostpopulargametitle;?></h3>
    <p>
    <?=$gamedescription;?>
    <br><?=$mostplayed;?> plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <?php if ($x ==8 || $x == 26) echo "";
    }
    ?>
    
    PHP:

    <div id="featuredb-1" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Test-game-1" title="Click to play Test game 1" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Test game 1</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-2" class="featuredbox">

    <a href="http://www2.skinzee.com/phpAS/games/Test-game-1" title="Click to play Test game 1" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Test game 1</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-3" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Test-game-1" title="Click to play Test game 1" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Test game 1</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>

    </div>
    <div id="featuredb-4" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Test-game-1" title="Click to play Test game 1" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Test game 1</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-5" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Test-game-1" title="Click to play Test game 1" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Test game 1</h3>
    <p>
    <br>0 plays

    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-1" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Santa" title="Click to play Santa" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Santa</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-2" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Santa" title="Click to play Santa" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Santa</h3>

    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-3" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Santa" title="Click to play Santa" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Santa</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-4" class="featuredbox">

    <a href="http://www2.skinzee.com/phpAS/games/Santa" title="Click to play Santa" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Santa</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-5" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Santa" title="Click to play Santa" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Santa</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>

    </div>
    <div id="featuredb-1" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Frogee" title="Click to play Frogee" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Frogee</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-2" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Frogee" title="Click to play Frogee" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Frogee</h3>
    <p>
    <br>0 plays

    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-3" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Frogee" title="Click to play Frogee" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Frogee</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-4" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Frogee" title="Click to play Frogee" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Frogee</h3>

    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>
    <div id="featuredb-5" class="featuredbox">
    <a href="http://www2.skinzee.com/phpAS/games/Frogee" title="Click to play Frogee" class="featured-1"><img src="http://www2.skinzee.com/phpAS/games/images/Frogeee.jpg" width="180" height="130" alt="" /></a>
    <h3>Frogee</h3>
    <p>
    <br>0 plays
    </p>
    <a href="#" class="playnow">Play Now! &raquo;</a>
    </div>


    It`s displaying everything 3x times.. :/ And i dont know why... Exactly...

    I`m willing to pay please PM me or post here your requested amount and your IM so we can talk LIVE!

    Thanks
     
    Neo_The_One, Dec 28, 2007 IP
  10. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Hi,

    I found out that the reason it`s showing up that many times is because of this loop:

    for ($i=1; $i<=5; $i++)

    It shows 5x times more then it should.. Is it possible to rewrite it?

    Thanks!
     
    Neo_The_One, Dec 28, 2007 IP
  11. jronmo

    jronmo Guest

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    All you need to do is simply return the query containing your top 5 games and loop them once using $x as the iterator to populate your div elements id's. You will want to ORDER BY "timesplayed" ASC so the loop starts with the most popular.
     
    jronmo, Dec 28, 2007 IP
  12. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Hi,

    Sounds simple and easy, but i dont know PHP other then the basic.

    Could you please take the code rewrite it and give me to workin code i will pay you just PM me please?

    Thanks!
     
    Neo_The_One, Dec 28, 2007 IP
  13. jronmo

    jronmo Guest

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Give this a try Neo,
    I changed the query to order by timesplayed DESC and put a condition in the loop to only show the first 5 iterations.

    
    <?php
    function display_todaysmostpopular($base_url,$rewrite,$sedir)
    	$sql_query = "SELECT * from games WHERE gamestatus = 1 ORDER BY timesplayed ";
    	//store the SQL query in the result variable
    	$result = mysql_query($sql_query);
    	if(mysql_num_rows($result))
    	{
    		//output as long as there are still available fields
    		$x=1;
    		while($row = mysql_fetch_array($result))
    		{
    			if($x <= 5)
    			{
    				$mostpopulargametitle = $row[gametitle];
    				$mostpopulargametitle = stripslashes($mostpopulargametitle);
    				$mostpopulargameid = $row[gameid];
    				$mostpopulartimesplayed = $row[timesplayed];
    				$gamedescription = $row[$gamedesc];
    				$sepopular = str_replace (" ","-",$mostpopulargametitle);
    				$sepopular= str_replace ("'","_",$sepopular);
    				$mostpopularicon = $row[gameicon];
    				$iconlocation = $row[iconlocation];
    				$mostplayed = $row[playedtoday];
    
    				// If SE Friendly is turned OFF, this will be displayed
    				if ($rewrite != 1)
    					$gameurl = $base_url."index.php?action=playgame&gameid=".$mostpopulargameid;
    
    				else
    					$gameurl = $base_url.$sedir."/".$sepopular;
    
    				if ($iconlocation ==1)
    					$gameicon = $mostpopularicon;
    				else
    					$gameicon = $base_url."games/images/".$mostpopularicon;
    
    				if ($x ==1 || $x == 9) echo "";
    ?>
    				<div id="featuredb-<?=$x?>" class="featuredbox">
    				                            <a href="<?=$gameurl;?>" title="Click to play <?=$mostpopulargametitle;?>" class="featured-1"><img  src="<?=$gameicon;?>" width="180" height="130" alt="" /></a>
    				                            <h3><?=$mostpopulargametitle;?></h3>
    				                            <p>
    				                                <?=$gamedescription;?>
    				                                <br><?=$mostplayed;?> plays
    				                            </p>
    				                            <a href="#" class="playnow">Play Now! &raquo;</a>
    				                        </div>
    <?php 
    				if ($x ==8 || $x == 26) echo "";
    			}else break;
    			$x++;
    		}
    	}
    }
    ?>
    
    Code (markup):
     
    jronmo, Dec 28, 2007 IP
  14. icfire

    icfire Peon

    Messages:
    321
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I have no way to test it but...

    function display_todaysmostpopular($base_url,$rewrite,$sedir){
    
    ?>
    
    <?
    $x = 0;
    $sql_query = "SELECT * from games WHERE gamestatus = 1 ORDER BY playedtoday DESC LIMIT 24";
    //store the SQL query in the result variable
    $result = mysql_query($sql_query);
    if(mysql_num_rows($result))
    {
    //output as long as there are still available fields
    ////////icfire edited here
    for ($i = 1; $i<=5; $i++)
    {
    $row = mysql_fetch_array($result);
    $x++;
    $mostpopulargametitle = $row[gametitle];
    $mostpopulargametitle = stripslashes($mostpopulargametitle);
    $mostpopulargameid = $row[gameid];
    $mostpopulartimesplayed = $row[timesplayed];
    $gamedescription = $row[$gamedesc];
    $sepopular = str_replace (" ","-",$mostpopulargametitle);
    $sepopular= str_replace ("'","_",$sepopular);
    $mostpopularicon = $row[gameicon];
    $iconlocation = $row[iconlocation];
    $mostplayed = $row[playedtoday];
    
    // If SE Friendly is turned OFF, this will be displayed
    if ($rewrite != 1)
    $gameurl = $base_url."index.php?action=playgame&gameid=".$mostpopulargameid;
    
    else
    $gameurl = $base_url.$sedir."/".$sepopular;
    
    if ($iconlocation ==1)
    $gameicon = $mostpopularicon;
    else
    $gameicon = $base_url."games/images/".$mostpopularicon;
    
    ?>
    <?if ($x ==1 || $x == 9) echo "";?>
    <div id="featuredb-<?=$i?>" class="featuredbox">
                                <a href="<?=$gameurl;?>" title="Click to play <?=$mostpopulargametitle;?>" class="featured-1"><img  src="<?=$gameicon;?>" width="180" height="130" alt="" /></a>
                                <h3><?=$mostpopulargametitle;?></h3>
                                <p>
                                    <?=$gamedescription;?>
                                    <br><?=$mostplayed;?> plays
                                </p>
                                <a href="#" class="playnow">Play Now! &raquo;</a>
                            </div>
    <?if ($x ==8 || $x == 26) echo "";?>
    <?
    
    
    }
    }
    
    ?>
    
    <?
    }
    
    
    ?>
    Code (markup):






    Before it was saying keep going until every row is looped through
    now it says just do 5

    Tell me if that works
     
    icfire, Dec 28, 2007 IP
  15. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #15
    Yours did`t work, just getting a blank page now for some reason i must have done something wrong problebly...

    If you want access to that folder to edit it your self please PM ME, and your price ! :)
     
    Neo_The_One, Dec 28, 2007 IP
  16. Neo_The_One

    Neo_The_One Active Member

    Messages:
    555
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #16
    icefire this works perfectly! ;)

    Thank you so much.. I really apprisiate it.. :)

    Could you help me out some more? Sending pm now...

     
    Neo_The_One, Dec 28, 2007 IP
  17. jronmo

    jronmo Guest

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I'll test it out. I have a working database to substitute for yours.
     
    jronmo, Dec 28, 2007 IP