going through arrays PHP [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 9 months ago.
I want know how i can extract the next 'team_name' from an array (30 difrent names) i know its probably simple, but I am beginer
when i using .$row0[$p]['team_name']. im geting this Warning: Trying to access array offset on value of type null in
$row0 = mysqli_fetch_array($sth0);
for ($i=1; $i <= $row['number_of_grups']; $i++)
{
echo "<div class='grup'> <p> GRUPA ".$i." </p>";
for ($a=0; $a < $teams_for_grup; $a++)
{
$team_input_name = "Druzyna" . $team_number_for_grups;
$team_number_for_grups++;
echo "<div class='teams'><a>".$row0['team_name']."</a><input type='number' class='points-box' name='".$team_input_name."' value='".$row0['points']."'</div>";
}
echo "</div>";
}

Without seeing the array for sure it will be hard to tell, but based off what I see here it looks like you would want to be doing:
$row[$a]['team_name']

Related

PHP zeropad (sprintf) AND number_format total [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
I have a $total with a value of 1400 which I'm trying to echo as 1,400.00 however sprintf and number_format refuse to cooperate with each other. How do I properly format the $total to echo as 1,400.00?
<?php
$total = 1400;
echo sprintf('%0.2f',$total);
//'1400.00'
echo number_format($total);
//'1,400'
echo sprintf('%0.2f',number_format($total_grand));
//'1.00'
echo number_format(sprintf('%0.2f',$total));
//'1,400'
?>
This should suffice:
$total = 1400;
echo number_format($total,2);
This will output:
1,400.00
See: number_format()

add numbers to new lines separated by comma [duplicate]

This question already has answers here:
PHP counter increment in a while loop
(5 answers)
Closed 2 years ago.
i am having some issues with my php code. what i wish to add numbers like before every lines something like below
1. some data
2. some more data
what i have tried doing is like below but it would not multiply the numbers like i want it to, it only prints 1
$num = 1;
$tdetails = $num++. str_replace(',', '<br />', $row['travellers_details']);
Could someone please show how to achieve what i am looking for?
thanks
You need to iterate and increment num each time. You can store the elements in an array first using explode:
$num = 1;
$tdetails = explode(',', 'some data, some more data');
$str = "";
for($i = 0; $i < count($tdetails); $i++)
$str .= $num++ . ". ". $tdetails[$i] . "<br>";
echo $str;
Output:
1. some data
2. some more data
You can make use of HTML ol(ordered list) tag which will automatically do the numbering for you.
<?php
$tdetails = "<ol><li>" . implode("</li><li>", explode(",",$row['travellers_details'])) . "</li></ol>";
echo $tdetails;

auto increment value in foreach loop, but it keeps going [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 3 years ago.
Summary: Trying to add +1 each time I go through my foreach loop, but it keeps going up even though I would only expect a maximum of 11 results in the loop
I tried to move around on the $i++; but without luck, I also check that I made sure the $i = 0; was outside of the foreach loop.
<?php
$facade = new NewsArticleFacade();
$news = $facade->getAll();
unset($facade);
$i = 0;
foreach($news as $new)
{
$i++;
if ($i = 5000000000000000)
{
echo "if statement: " . $i;
break;
}
$content = substr($new->getContent(), 0, 69);
echo "<div class='news'>";
echo "<h4><a href='/news/news.php?id=".$new->getNewsID()."'>".$new->getTitle()."</a></h4>";
echo "<span>by <a href='#'>".$new->getUser()->getUsername()."</a> - ".$new->getDate()->format("d-M-Y")."</span>";
echo "<p>".$content." ...</p>";
echo "<div class='gradient'></div>";
echo "<img src='img/news/0000000001.jpg'>";
echo "</div>";
}
?>
What really confuses me is if I remove the if statement for the loop, and just echo $i each time, it will echo out 11 in total, what am i doing wrong with my if?
I would expect the loop to go through 11 times since I have 11 entries in the database for this selection, but it keeps going up, if I echo within the if statement i can see the value will just be whatever i limit the if statement to be.
Thanks in advance!
This
if ($i = 5000000000000000)
Is assignment and it makes little sense as such in your code. You probably wanted to have comparison operation which is expressed with ==
if ($i == 5000000000000000)

Echo out html set number of times - php [duplicate]

This question already has answers here:
make string of N characters
(5 answers)
Closed 2 years ago.
I'm creating quite a complex html <table> layout and at this early stage it quite time consuming for me to copy and paste each <tr> in order to generate dummy content.
My idea was to specify a dummy <tr> as a $var and then output that x number of times using a function as below:
$html = "<tr>//content</tr>";
function dummy_html($html, $times){
$i = 0;
for ($i <= $times) {
echo $html;
$i = $i++;
}
}
echo dummy_html($html, 5);
But this is returning a parse error on the for line any idea why that might be ?
PHP has a function already
echo str_repeat($html,5);
Your for loop is incorrect. It should be something like:
for( $i = 0; $i <= $times; $i++ ) {
echo $html;
}
Update
#Your Common Sense's solution is better: str_repeat (http://php.net/manual/en/function.str-repeat.php)
http://php.net/manual/en/control-structures.for.php
for should use the notation: for (set arguments, conditions, command to run at the end of the loop), therefor should be:
for($i = 0; $i <= $times; $i++)
Also, I would recommend using str_repeat (http://php.net/manual/en/function.str-repeat.php)

PHP foreach add code for every 4 results found [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
On every third iteration in PHP
Using PHP foreach to get a series of results,how would I add the following echo '<div class="clear"><div>' every 4 results
Any ideas?
Using modulos can do the trick :
$i = 1;
foreach($data as $result) {
if($i % 4 === 0) {
echo '<div class="clear"></div>';
}
$i++;
}

Categories