/    Sign up×
Community /Pin to ProfileBookmark

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:

[code=php]
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
[/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>
[/CODE]

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

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@BezzzZFeb 19.2014 — interesting, can you post the full code ?
Copy linkTweet thisAlerts:
@almehtabshaikhFeb 19.2014 — good work dude i impress you
Copy linkTweet thisAlerts:
@almehtabshaikhFeb 19.2014 — some more query are required so mention the link below come with my site and you get some more details for language [url=http://www.logodesign.ae]Website Maker[/url]
Copy linkTweet thisAlerts:
@thewebiphyerauthorFeb 20.2014 — 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
Copy linkTweet thisAlerts:
@NogDogFeb 20.2014 — 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.
Copy linkTweet thisAlerts:
@BezzzZFeb 24.2014 — because the issue may be elswhere, I can't see why it would be doing anything in this code you have posted
Copy linkTweet thisAlerts:
@thewebiphyerauthorFeb 25.2014 — 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
Copy linkTweet thisAlerts:
@thewebiphyerauthorMar 03.2014 — 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.[/QUOTE]


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
[code=php]
<?php foreach($posts as $post){
if($post['format']=='vid'){
include('inc/vid.php');
}else{
include('inc/artcl.php');
} ?>
<?php } ?>
[/code]


here are the includes:

vid.php
[code=php]
<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>
[/code]


and artcl.php:
[code=php]
<?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>
[/code]
Copy linkTweet thisAlerts:
@thewebiphyerauthorMar 06.2014 — really? noone? I also found out that my problem only happens when certain users are logged in... REALLY confusing
Copy linkTweet thisAlerts:
@thewebiphyerauthorMar 08.2014 — well okay then if no one is going to respond then forget it... I'll just unsubscribe from this i guess
Copy linkTweet thisAlerts:
@Error404Mar 08.2014 — 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.
×

Success!

Help @thewebiphyer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...