www.webdeveloper.com
Results 1 to 10 of 10

Thread: foreach loop confusion, condition passing but code doesn't render?

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81

    foreach loop confusion, condition passing but code doesn't render?

    I have a page where I'm using a foreach loop to cycle through users posts. The code for the text posts are rendering just fine, but what I don't get is a weird glitch in the video submissions.

    here is the code I wrote:
    PHP Code:
    if($post['format']=='vid'){ ?>
              <li class='vid'>
                <video class='post-video' height='300' width='400' controls>
                  <source src='<?php echo $post['entry_path']; ?>'></source>
                </video>
                <br>Posted by: <?php echo $post['username']; ?>
          <?php if(isset($_COOKIE['goodact']) && $u_logged!=$post['username']){ ?>
                  <form action='scripts/vote.php?member=<?php echo $post['username']; ?>&logged=<?php echo $u_logged?>&page=<?php echo $page?>'
                    method='post'>
                    <input type='hidden' name='entry_id' value='<?php echo $post['id']; ?>'>
                    <?php if(!in_array($post['id'], explode(",",$votes))){ ?>
                      <input type='submit' value='GOOD CATCH!'>
                    <?php }else{ ?>
                      YOU VOTED THIS POST A GOOD CATCH.
                    <?php ?>
                  </form>
          <?php ?>
              </li>
      <?php //more code
    but here is the code rendered in the browser when a user is logged in. Keep in mind the difference between the following two video items:
    Code:
    <li class="vid">
                <div class="RDVideoHelper" style="position: absolute; z-index: -1; display: block;"><object class="RDVideoObject" type="application/x-rn-downloaderhtml5videoshim-plugin" src="http://localhost/goodact/entries/videos/thepulsiphyer/Patterns%20of%20Light%20part%20II.mp4" width="400" height="300" data-offsetleft="143" data-offsettop="424"></object></div><video class="post-video" height="300" width="400" controls="">
                  <source src="entries/videos/thepulsiphyer/Patterns of Light part II.mp4">
                </video>
                <br>Posted by: thepulsiphyer                    
                    <input type="hidden" name="entry_id" value="6">
                                      <input type="submit" value="GOOD CATCH!">
                                  
                    </li>
    <li class="vid">
                <div class="RDVideoHelper" style="position: absolute; z-index: -1; display: block;"><object class="RDVideoObject" type="application/x-rn-downloaderhtml5videoshim-plugin" src="http://localhost/goodact/entries/videos/nadiene38/hour%20by%20hour%20lds.org.mp4" width="400" height="300" data-offsetleft="143" data-offsettop="787"></object></div><video class="post-video" height="300" width="400" controls="">
                  <source src="entries/videos/nadiene38/hour by hour lds.org.mp4">
                </video>
                <br>Posted by: nadiene38                    <form action="scripts/vote.php?member=nadiene38&amp;logged=notdross99&amp;page=index" method="post">
                    <input type="hidden" name="entry_id" value="7">
                                      <input type="submit" value="GOOD CATCH!">
                                  </form>
                    </li>
    what's wrong with the first list item? why is the form showing up for the second one but not the first? I would think that since the <input> element in the first video appears that means the first if(isset($_COOKIE['goodact']) && $u_logged!=$post['username']) has passed. But since the <form> element didn't render in the first one, that would mean that initial condition didn't pass right? the code seems to be contradicting itself here... so confused

  2. #2
    Join Date
    Feb 2014
    Location
    south africa
    Posts
    16
    interesting, can you post the full code ?

  3. #3
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    before I do... is the code I have posted not providing sufficient details to find the solution? I get very protective of my code so I don't like posting more than is absolutely necessary

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,312
    What are you actually looping on?

    Best guess would be that $post['username'] is not the same on each iteration, but there's no way to be sure without seeing the source of those values and whether anything else in the code is messing with them. You could add some error_log() or other debug code to see what's in $post right before that check, as well as any other variables of interest.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Feb 2014
    Location
    south africa
    Posts
    16
    because the issue may be elswhere, I can't see why it would be doing anything in this code you have posted

  6. #6
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    sorry it took me so long to respond... I will be looking at what you said nogdog... I have a lot of projects going.. I'll let you know what i find

  7. #7
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    Quote Originally Posted by NogDog View Post
    What are you actually looping on?

    Best guess would be that $post['username'] is not the same on each iteration, but there's no way to be sure without seeing the source of those values and whether anything else in the code is messing with them. You could add some error_log() or other debug code to see what's in $post right before that check, as well as any other variables of interest.
    I am iterating on a multidimensional array which contains information about users posts.

    I took at look at $post['username']... I'm not sure if the problem I'm having lies in the $u_logged!=$post['username'] condition or elsewhere... I echoed out both variables on each iteration of that type and each time it ends up being true. So I'm still confused about why the <form> element isn't rendering while the <input> element is.

    anyways... here's the whole thing I'm talking about... this is the loop
    PHP Code:
    <?php foreach($posts as $post){
              if(
    $post['format']=='vid'){ 
                include(
    'inc/vid.php');
              }else{
                include(
    'inc/artcl.php');
              } 
    ?>
      <?php ?>
    here are the includes:
    vid.php
    PHP Code:
    <li class='vid'>
          <video class='post-video' onmousedown='mouseDown(event)' controls>
            <source src='<?php echo $post['entry_path']; ?>'></source>
          </video>
          <br>Posted by:
            <?php if(isset($u_logged)){ ?>
              <a href='profile.php?user=<?php echo $post['username']; ?>'>
            <?php ?>
              <?php echo $post['username']; ?>
            <?php if(isset($u_logged)){ ?>
              </a>
            <?php ?>
    <?php 
    if(isset($_COOKIE['cookiename']) && $u_logged!=$post['username']){?>
            <form action="scripts/vote.php?member=<?php echo $post['username']; ?>&logged=<?php echo $u_logged?>&page=<?php echo $page?>"
              method='post'>
              <input type='hidden' name='entry_id' value='<?php echo $post['id']; ?>'>
              <?php if(!in_array($post['id'], explode(",",$votes))){ ?>
                <input type='submit' value='GOOD CATCH!'><?php /*echo $post['username']." ".$u_logged;*/ ?>
              <?php }else{ ?>
                YOU VOTED THIS POST A GOOD CATCH.
              <?php ?>
            </form>
    <?php ?>
        </li>
    and artcl.php:
    PHP Code:
    <?php $path_array=explode("/"$post['entry_path']);
              
    $title_arr=explode("."$path_array[3]);
              
    $title=$title_arr[0];
              
    $file fopen($post['entry_path'], 'r'); $content fread($file,200);
              
    fclose($file);?>
              <li class='txt'>
                <a class='post-text-link'
                  href='JavaScript:showarticle("<?php echo $post['entry_path'];?>","<?php echo $title?>")'><?php echo strtoupper($title); ?></a>
                <br><p class='post-text-prev'><?php echo $content."..."?></p>
                  Posted by: 
                  <?php if(isset($u_logged)){ ?>
                    <a href='profile.php?user=<?php echo $post['username']; ?>'>
                  <?php ?>
                    <?php echo $post['username']; ?>
                  <?php if(isset($u_logged)){ ?>
                    </a>
                  <?php ?>
          <?php if(isset($_COOKIE['cookiename']) && $u_logged!=$post['username']){ ?>
                  <form action='scripts/vote.php?member=<?php echo $post['username']; ?>&logged=<?php echo $u_logged?>?page=<?php echo $page?>' method='post'>
                  <input type='hidden' name='entry_id' value='<?php echo $post['id']; ?>'>
            <?php if(!in_array($post['id'], explode(",",$votes))){ ?>
                    <input type='submit' value='GOOD CATCH!'></form>
            <?php }else{ ?>
                    YOU VOTED THIS POST A GOOD CATCH.
            <?php ?>
          <?php ?>
              </li>
    Last edited by thewebiphyer; 03-03-2014 at 01:16 PM. Reason: including more code

  8. #8
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    really? noone? I also found out that my problem only happens when certain users are logged in... REALLY confusing

  9. #9
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    well okay then if no one is going to respond then forget it... I'll just unsubscribe from this i guess

  10. #10
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    From what I can tell, some of your code is redundant, although it's unlikely to be the source of the problem. For example, in vid.php and articl.php, you have 2 if(isset($u_logged)) one after the other. If lots of your code is like this, it'll be harder for you to read and may be a possible source for a problem.

    In terms of only having certain users experiencing difficulty, I would look at how you're storing the username and passing it across pages. Personally, I prefer sessions due to their simplicity but I don't see any session or cookie that's storing the username, perhaps it's elsewhere in your code. To see where it's no longer being retained, echo the username. If it is being retained properly, then the next step is to look at whether there is something about the problematic usernames, such as whether there's a space, other special characters, numbers, etc... . If you're processing the username in some way, then this may throw it off.

    As for not wanting to post all of your code, I understand and it's very well appreciated to only post a relevant snippet.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles