Need help with link page.

Discussion in 'PHP' started by Gfer, Sep 24, 2008.

  1. #1
    Hey there, I need some help with my php script. It's designed to put in entries/links under a headline which is the day they were added into the database. So it looks like this for example:

    2008-09-25
    entry4
    2008-09-24
    entry3
    entry2
    entry1

    and so on, however my problem is that I can't figure out a way to put a </div> where the entries stop for the day. I wan't it to be so that I can manipulate all the entries for one day seperately and so on. Here's my code.

     $olddate= "";
    ?>
    <div class="holder2">
    <?php
    		while($row = mysql_fetch_array($result)) {	
    			list($id, $rubrik, $embedurl, $lankurl, $klick, $date, $tid, $kat, $tipsare) = $row;
    
    			$qcount = mysql_query("SELECT * FROM kommentarer where nid='".$row['id']."'");
    			$scount = mysql_num_rows($qcount);
    
    		if($olddate != $date) {
    		$olddate = $date;
    ?>
    <div class="dateholder"><p><?php echo $date; ?></p></div>
    	<?php
    							          } // end if
    
    	?>
    	<div class="linkholder">
    		<ul>
    
    <?php
    		if($embedurl != '') {
    ?>
    		<li><a href="lank.php?id=<?php echo $id; ?>" target="_blank"><?php echo $rubrik;?></a></li>
    <?php
    					}  else {
    ?>
    		<li><a href="redir.php?id=<?php echo $id; ?>" target="_blank"><?php echo $rubrik;?></a></li>
    <?php
    }
    ?>	
    	  <li><small><?php echo $tid;?></small></li>
    		<li><a href="#" onClick="window.open('kommentar.php?id=<?php echo $id; ?>','mywindow','width=350,height=400')"><?php echo $scount;?> Inlägg</a></li>
    		<li>Klick: <?php echo $klick;?></li>
    		<li>Kategori: <?php echo $kat;?></li>
    		<li>Tipsare: <?php echo $tipsare;?></li>
    		<li>Rating: <?php echo rating_bar($id,'5'); ?></li>
    	
    	  </ul>
    		</div>
    
    <?php 
    	if($olddate != $date) {
    ?>
    
    <h1>TEST</h1>
    <?php
    	}
    	} // end while 
      }
    ?>
    </div>
    PHP:
     
    Gfer, Sep 24, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    erm

    if($olddate != $date) {
    $olddate = $date; ...

    so it goes in and sets to same

    then at bottom you go:

    if($olddate != $date) {
    ?>

    <h1>TEST</h1>
    <?php
    }

    even if they did differ, they don't anymore. you kind of need to move this print above the first if .
     
    dimitar christoff, Sep 24, 2008 IP
  3. Gfer

    Gfer Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, I know, that is the version of the code that doesn't work. Forgot to mention that, sorry :p
     
    Gfer, Sep 24, 2008 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    well, you can do it before :)

    something like
    
    <? 
    
    $first = true;
    while ( ) {
    ...
    if ($olddate != $date) {
        if ($first) 
            // on the first loop iteration, don't close a layer (not open yet)
            $first = !$first;
        else 
            echo '</div>'; 
    
        echo '<div class="dateholder"><p>...';
    } // end while
    
    echo "</div>"; // close last div.
    
    ?>
    PHP:
     
    dimitar christoff, Sep 24, 2008 IP
  5. Gfer

    Gfer Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks! I'll see if it works later. Looks promising though. Why is the first </div> right after else in there though? Is that the one that closes the last entry for the previous day?
     
    Gfer, Sep 24, 2008 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    correct. but on the very first loop iteration, there has not been a previous entry. this will prevent you form closing the parent div inadvertently :)
     
    dimitar christoff, Sep 24, 2008 IP
  7. Gfer

    Gfer Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok, so I shouldn't put anything in the first iteration but the $first = !$first; line, right?
     
    Gfer, Sep 24, 2008 IP