For loop through an array PHP [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to loop through an array and create 8 variable p1, p2, p3 etc.
<?php
for ($z =0; $z < 7; $z++) {
echo ' var p '.($z + 1).$gamelistarray[$z+1][4]. "<br>";
}
?>
This is the code that I have but I dont know how to progress it or what is wrong?

If you're trying to make JavaScript varibales p1 through p8 and assign a value to them, I would do this:
<?php
for ($z=1; $z < 8; $z++) {
echo "var p$z = '{$gamelistarray[$z][4]}';\r\n";
}
?>
Faster and flexible:
<?php
$z = 1;
foreach($gamelistarray as $gl){
echo "var p$z = '{$gl[4]}';\r\n";
$z++;
}
?>

Related

better way with Simple HTML Dom Parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
my HTML code is THE CODE IS REPEATED 16 times :
<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>
I WANT TO GET all the imgs links and text also href what i did :
for ($x = 0; $x <= 15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
echo '<br/>';
echo $mytext;
echo '<br/>';
print_r($postlink);
echo '<br/>';
}
the code is slow any changes ?
You slow down your code by using too much anonymous objects. It means you don't put the result of the function into a variable, rather just use it "on the go". This needs to run your function again and again slowing down your project.
Because you can use the function find to return an array, I advice you to do so before the for loop.
$imgarray = $html->find('div[class=headline_image] img', $x);
This way you run $html->find exactly once, and not sixteen times. In the for loop you can use it as an array and work with the results: $imgarray[$x]. You make the same for $anchorarray and your code will speed up, you'll see.
Alternative solution is using PHP DOM $childNodes on the container in which this 16 item can be found (or the body element). This will return the sixteen div elements in which you can navigate by calling $firstChild for the <a> element and $firstChild again for the <img> element. Probably this is more secure in case you want to make changes to the website (like adding more content to the end etc.)
Hey Daniel i changed the code to :
$imgarray = $html->find('div[class=headline_image] img');
$linkarray = $html->find('div[class=headline_image] a');
for ($x = 0; $x <= 15; $x++) {
echo $imgarray[$x]->getAttribute('src');
echo '<br/>';
echo $imgarray[$x]->getAttribute('alt');
echo '<br/>';
echo $linkarray[$x]->getAttribute('href');
echo '<br/>';
}
In general the proper way to iterate looks like this:
foreach($html->find('div') as $div){
echo $div;
}

Infinite loop. Why? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i wanna test if my variable is empty or not to display some differents things.
When i don't use the else...if everything work but when i use this code :
<?php
$Amazon = get_post_meta($post->ID, "Lien Amazon", true);
?>
<?php
if( $Amazon != NULL ){
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;}
else {
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;}
?>
What is the problem ? Thank you
Here is an output error. You haven't closed and reopened the string on trying to concat a variable.
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;
Do instead:
echo '<li><span class="post-meta-key">Acheter sur Amazon</li>' ;

Clean HTML and PHP For Loop Not Displaying Anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<?php for($x = 0; $x <= count($slides); $x++):?>
<li data-target="#main-carousel" data-slide-to="<?php echo($x);?>'" class="active"></li>
<?php endforeach; ?>
Not exactly sure what the error is everything seems right.
You start yourself with a for loop, while closing it with a foreach loop. These are two very different things and cannot be matched like that.
You simply need to replace
<?php endforeach; ?>
with
<?php endfor; ?>
Alternatively, you can use curlybrackets { instead, having something like
<?php for($x = 0; $x <= count($slides); $x++) { ?>
<!-- do HTML here -->
<?php } ?>

Else status in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I would need some help, since this code does not work (I have lack of knowledge, but I need to have it in that format)
$user_goals is array()
$user_count is just a number like 15
<?php foreach ($user_goals as $number): ?>
<?php if ($number < $user_count): ?>
<h5 class="text-center"><strike><?php echo htmlspecialchars($number); ?> users</strike></h5>
<?php else: ?>
<h5 class="text-center"><?php echo htmlspecialchars($number); ?> users</h5>
<?php endif; ?>
<?php endforeach; ?>
Any help will be apreciated :)
result that I want:
$user_count = 15;
$user_goals = array(
10,
15,
20,
etc,
);
so I want to get:
<strike>10</strike><br>
<strike>15</strike><br>
20
Solved it myself
<?php
foreach ($user_goals as $number) {
if ($number <= $users_count) {
echo "<h5 class='text-center'><strike>$number users</strike></h5>";
}else{
echo "<h5 class='text-center'>$number users</h5>";
}
}
?>
If you are looking to add a strike through when the goal is less than or equal to, you need to use that comparison operator.
if ($number <= $user_count)

using php function to allow users to select their dob from drop down list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I saw from online somewhere a method to let user select their DOB using a Drop down list using php function. This is a selection of it:
<select name="monthOfBirth">
<option value="">---Select month---</option>
<?php for ($i = 1; $i <= 12; $i++) : ?>
<option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
I tried it and it works, but can anybody explain to me what this does:
<option value="<?php echo ($i < 10) ? '0'.$i : $i; ?>"><?php echo $i; ?></option>
I know it will list down the date but I do not understand the ..?'0'.$i ; $i;.. part. Can anybody explain to me?
It is a short if/else:
// This
echo ($i <10) ? '0'.$i : $i;
// Is the same as this:
if( $i <10){ echo '0'.$i;}
else{ echo $i;}
It adds a zero if less than 10.
You could do this too:
if( $i <10){ echo '0';}
echo $i;
Edit: You wrote $1 instead of $i in your code, and got a > where a < was needed

Categories