Get first element Array in for loop - php

I am trying to retrieve the last names of the orders placed.
code:
for ($x = 0; $x < 25; ++$x) {
if (($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == TRUE)) {
echo '<pre>';
print_r($orders[$x]['lastname']);
echo '</pre>';
}
}
question: code is not showing me the first lastname in the array actually the name that is in position $orders[0]. I thought that pre-incrementing the variable like this ++$x like this would solve the problem. To bad thats not the case. Any tips?
Update output:
[1]""
[2]""
[3]""
[4]""
[5]""
Not sharing lastnames, which is also not relevant.

try the following to confirm which element of the array you are displaying (and the value of $x
for($x = 0; $x < 25 ; $x++){
if(($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == true)){
echo '<pre>';
--print_r($orders[$x]['lastname']);
echo '['.$x.']'.$orders[$x]['lastname'];
echo '</pre>';
}
}

Related

How can I make a for statement have an else

I have a databse that has food reviews on it, and I want to show if a food is vegetarian or not.
I want to add a echo "No"; but I don't know how.
I've tried using else, but it doesn't show. I've also tried <?php for($x=1;
Here is my code:
<p>Vegetarian: <span class="sub_heading"><?php for($x=0; $x < $find_rs['Vegetarian']; $x++)
{
echo "Yes";
}
; ?></span></p>
I want it to show no if it is 0 and yes if it is 1
<p>
Vegetarian:
<span class="sub_heading"><?= $find_rs['Vegetarian'] ? "Yes" : "No"?></span>
</p>
Simply add an if conditional inside of your loop that checks against the value of $x:
for ($x=0; $x < $find_rs['Vegetarian']; $x++) {
if ($x == 1) {
echo "Yes";
}
else if ($x == 0) {
echo "No";
}
else {
echo "x wasn't either 0 or 1 -- x was $x";
}
}
Note that the final else condition will output the value of $x due to the use of double-quotes.
if the condition only 2, just using if else statement,
<?php
for($x=0; $x < $find_rs['Vegetarian']; $x++){
if($x == 1){echo 'Yes';}else{echo 'No';}
}
?>

Star rating with half star support

I am running into a problem rendering a star rating and was hoping I can get some extra eyeballs on my problem. I have the normal rating working just fine with whole numbers but I am struggling to display the half star. For example I created a service that provides me with a rating 0-5 so I get a value like 2.5, 3 or 5 etc...
Before I go and create a switch case and create an svg for each variation I was hoping to get a little a pointer. Below is what I have currently, any tips would be greatly appreciated.
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
}
else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
Ideally I would like to add a condition at the end of the loop and check for the half and echo "";
There is probably an easier way to do it but this works, checks if $starRating is a float and then rounds it up and checks against $i to place the half star in the correct position.
<?php
$totalRating = 5;
$starRating = 2.5;
for ($i = 1; $i <= $totalRating; $i++) {
if($starRating < $i ) {
if(is_float($starRating) && (round($starRating) == $i)){
echo "<img src=\"/icons/star-half.svg\">";
}else{
echo "<img src=\"/icons/star-empty.svg\">";
}
}else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
You can verify if the value of $starRating is an integer, doing something like this (considering only half values):
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star_half.svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
If you want to show stars with a more precise value you can create images with the float values in the name, like "star-3.svg" (representing a .3 float value, and do something like this:
<?php
for ($i = 1; $i <= $totalRating; $i++) {
if ($starRating < $i ) {
echo "<img src=\"/icons/star-empty.svg\">";
} elseif(is_int($starRating) === false) {
echo "<img src=\"/icons/star-" . floatval($starRating)) . ".svg\">";
} else {
echo "<img src=\"/icons/star.svg\">";
}
}
?>
But in this case you need to take care to only receive float values with one number (2.2, 3.4, etc.).
I hope it helps...

php First value of array not displaying

I have the following bit of code and for some reason in the first WHILE loop the first value of $actor_list (when $i = 0) does not display. If I simply echo $actor_list[0] then it displays fine, but in the loop it will not display. I merely get [td][/td] as the output. The remaining values of the array display fine.
Also the line
echo '</tr><tr> </br>';
is not displaying.
What am I missing here? The value of $num_actors is an even number in my test scenario so there doesn't seem to be a reason for the above echo line to be skipped.
$actor_list = explode(" ", $actors);
$num_actors = count($actor_list);
if ($num_actors <= 6) {
foreach ($actor_list as $actor) {
echo '[td]'.$actor.'[/td] </br>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
echo '</tr><tr> </br>';
while ($i <= $num_actors) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
}
}
You're not initialising the variable $i to 0, which means it will be set to 'null' and thus not reference the 0th index of the array; but when it's then incremented its value will become 1.
Note: [..] Decrementing NULL values has no effect too, but incrementing them results in 1.
See: http://php.net/manual/en/language.operators.increment.php
Try adding:
$i = 0;
before the if statement.
Made a few changes.
This is working for me.
Try it out.
<?php
$actor_list = 'act1 act2 act3 act4 act5 act6';
$actors = explode(" ", $actor_list);
$num_actors = count($actor_list);
//debug
//echo "<pre>";
//print_r($actor_list);
//echo "</pre>";
echo "<table>";
if ($num_actors <= 6) {
foreach ($actors as $actor) {
echo '<tr><td>'.$actor.'</td></tr>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
while ($i <= $num_actors) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
}
}
echo "</table>";
?>

Count elements in a loop

I'm trying to count elements in a loop to break each number of elements and show in groups
My little script
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if ($x%5 == 0)
{
print "<br>";
}
}
As you can see in the little script each 5 rounds I want show the tag for break and show as in groups of elements.
The problem it´s always show in the first line one element and after this the rest, and no works fine.
The index of $exp_filter starts at 0, so this block of code
if ($x % 5 == 0)
{
print "<br>";
}
should be
if (($x+1) % 5 == 0)
{
print "<br>";
}
Here's the complete modified code
$data = "house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if (($x + 1) % 5 == 0)
{
print "<br>";
}
}
Working example: http://codepad.org/iEsKK98M
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x%5==0)
{
print "<br>";
}
}
Try this. The problem is you started at 0 in for, you should start from 1 ;)
Quickfix:
Demo
<?php
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x > 0 && $x%5==0)
{
echo "<br />";
}
}
?>
I would use array chunk and implode instead:
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
foreach(array_chunk(explode('*', $data), 5) as $chunk){
echo implode(' ', $chunk) . '<br>';
}
Live example: http://codepad.viper-7.com/ED2wHR

PHP Infinite While Loop, Unknown Cause

<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i==1 || ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i=$page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Trying to build a webpage that ouputs some search values neat the bottom of the page, however I keep getting an infinite loop. I've no Idea about what's causing it, and would like someone elses insight into the problem at hand.
Your if condition has a problem
Change
$i =1
with
$i==1
You have a number of places that you reset $i, inadvertently I assume
$i=1
and
$i=$page
replace them with ==
<?php
$i=1;
while ($i<=$totalpages) {
if (($i>=($page-5) && $i<=($page+5) && $i<$totalpages) || $i == 1|| ($i+1>$totalpages)) {
echo "<td><a href=\"search.php?page=$i\">";
if ($i == $page) {
echo "<strong> $i </strong>";
}
if ($i!=$page) {
echo " $i ";
}
echo "</a></td>";
}
$i++;
}
?>
Changes in 1st and second if condition.
1) $i == 1
2) $i == $page
is there a reason why this can not be accomplished using a for loop ? While loops are always risky and easy to cause problems if there is a scenario to cause to loop for ever.
eg.
for ($i = 0; $i<=$totalpages; $i++){
// do something
}
There is some issues in the conditions
first: the
|| $i=1||
allways return true.
Change for
|| $i==1||
Second: The same case of adobe
if ($i=$page) //Allways return try is assignation operation
Change to:
if ($i==$page)

Categories