Show only 100 times one row.

Discussion in 'PHP' started by krisis32, Aug 25, 2012.

  1. #1
    Hello there. How can i make a limt of showing the results?
    I need to limit it for 100 views.
    In DB i have ID|NAME|PAGE|COUNT|DATE

    In count i want to count till 100 and then stop showing that ID.
    I could do it with count < 100 .
    And then update the specific ID. I could get records with less than 100 views but i couldnt manage to update count on the specific ID.
    Row is showed with:

    foreach($bannerGroups[0] as $ban)        {            echo '<li class="right1">'.$ban->html().'</li>';        }
    PHP:
    But i just dont know where to put the update in there. I tried but all i got was to update only one ID. But it shows 4 on one page and randomizes them on refresh. So i dont know what to do.
    Also i would like to say i a only learning php.
    Sorry for all the mess.

    Code at pastebin . com / A9hJTPLE
    (link must be together without spaces)
     
    Last edited: Aug 25, 2012
    krisis32, Aug 25, 2012 IP
  2. InstaCoders

    InstaCoders Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might want to consider using a session count and ip matching / username matching that would help to stop someone from viewing more than x amount of times.

    Write yourself a function that checks the user against a table of user/ip views, pass the user to the function check it, get a returned value (#) and if it's 100 stop showing it.
     
    InstaCoders, Aug 25, 2012 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    If you only want 100 of them, why aren't you using LIMIT on your query?

    The rest of your post... I can't even make heads nor tails of what you are even trying to do... Your code... only accesses the first for, then tries to do an invalid foreach on them... makes no sense.... the array_chunk might make sense if it was doing 4, not one... but otherwise that's just returning the same array as the one you feed it.

    $bannerGroups = array_chunk($banners,1);

    means $bannerGroups == $banners

    That part makes no sense, other than wasting memory on nothing. What I THINK you want to do, is array_chunk($banners,4) to make your four LI that you foreach...

    Something more like:

    
    error_reporting(E_ALL^E_NOTICE);
    
    // adding LIMIT means it will only show you the first 100
    $bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' LIMIT 100");
    
    if (mysql_num_rows($bannerResult)==0) {
      echo 'empty';
    } else {
    	$banners=array();
    	while($row=mysql_fetch_assoc($bannerResult)) {
    		$banners[] = new Banner($row);
    	}
    
    	shuffle($banners);
    	$bannerGroups=array_chunk($banners,4);
    	foreach ($bannerGroups as $key => $group) {
    		foreach ($group as $ban) {
    			echo '<li',(
    				$key%2==1 ? ' class="even"' : ''
    			),'>',$ban->html(),'</li>';
    		}
    	}
    
    }
    Code (markup):
    Though I'm still not 100% sure I'm following what you're even trying to do. BTW, avoid wasting classes on elements that don't need them, only use a class when they're different, if you have two states, only one of them needs a class!

    Oh, and NOT that you should even be using mysql_ functions in the first place, given they are deprecated in favor of mysqli or PDO. (I prefer PDO)

    Though if you want just the first random 100 out of the entire data set? That's problematic since you'd have to pull the entire set first, a MASSIVE waste of time and memory... I'd probably pull a count in that case, do a random offset on the limit based on a multiple of the count. That would give you a random 'page of 100' in random order -- each 'page' would have the same items when it comes up, but it would be far less strenuous on the server.
     
    deathshadow, Aug 25, 2012 IP