I had 14 li elements with various information in and thought it's be better to wrap it all up into a php loop and use variables and arrays to fill in the gaps.
I've encountered two problems. The first of which is that I'm not returning any elements from the array description or title.
The next problem is in the filename $iFINAL.pdf - it should just be the variable $i with FINAL appended to it.
I wouldn't normally use EOT but in this case it seemed far quicker than escaping all the various quotes.
any help is appreciated, thanks!
<?php
$description = array("Decription 1 here","description 2 here");
$title = array("title 1","titlesfdfs ","sdfsdsd","wqeqe","","");
for($i=1; $i <= 14; $i++){
if($i < 10){
$i = "0".$i;
}
$body = <<<EOT
<h3>Chapter $i - $title[$i]</h3>
<div class=trainingItemListContainer>
<div class="mainDetails">
<p><strong>Introduction:</strong> $description[$i]</p>
</div>
<div class="subDetails">
<div class="viewAndDownload">
<p>Click to view the chapter</p>
</div>
<div class="viewAndDownload">
Click to download the PDF file <img src="../images/disk.png" alt="downloadIcon" border="0"/>
</div>
</div>
</div>
EOT;
echo $body;
}
There are two major issues with your code:
You're setting the value of $i to 01, 02 etc. This will cause the script to produce Undefined Index errors because there's no such index in your array.
You're not enclosing the variables in { }. If you enclose it, the actual variable values will be used. For example: {$i}
Example:
$body = <<<EOT
<h3>Chapter {$i} - {$title[$i]}</h3>
<div class=trainingItemListContainer>
<div class="mainDetails">
<p><strong>Introduction:</strong> {$description[$i]}</p>
</div>
<div class="subDetails">
<div class="viewAndDownload">
<p>Click to view the chapter</p>
</div>
<div class="viewAndDownload">
Click to download the PDF file <img src="../images/disk.png" alt="downloadIcon" border="0"/>
</div>
</div>
</div>
EOT;
Several issues here:
(1) This code is causing problems:
if($i < 10){
$i = "0".$i;
}
WHY? if you did a var_dump($i) (= simple debugging), you'd realize that
$title[1] is different from $title['01']
solution: delete the above code.
(2) an array in PHP will start with index 0, not 1.
echo $description[1];
will output "description 2 here".
solution:
for ($i = 0; $i < 14; $i++) {
$number = str_pad(($i + 1), 2, "00", STR_PAD_LEFT);
...
<h3>Chapter $number - $title[$i]</h3>
see it working here: http://codepad.viper-7.com/489laJ
There is a problem within your code, you are changing the value of $i (reassigning another value with it here):
if($i < 10){
$i = "0".$i;
}
You can alternatively use another variable such as $j
if($i < 10){
$j = "0".$i;
}else{
$j = $i;
}
This might not be your exact problem, but it might help you to improve your code.
Why not try this?
<?php
$description = array("Decription 1 here","description 2 here");
$title = array("title 1","titlesfdfs ","sdfsdsd","wqeqe","","");
for($i=1; $i <= 14; $i++){
if($i < 10){
$i = "0".$i;
}
?>
<h3>Chapter <?php print($i." - ".$title[$i]); ?></h3>
<div class=trainingItemListContainer>
<div class="mainDetails">
<p><strong>Introduction:</strong> <?php print($description[$i]); ?></p>
</div>
<div class="subDetails">
<div class="viewAndDownload">
<p>Click to view the chapter</p>
</div>
<div class="viewAndDownload">
Click to download the PDF file <img src="../images/disk.png" alt="downloadIcon" border="0"/>
</div>
</div>
</div>
If you have PHP code later on, just open the <? tag again!
Related
For parsing a list of articles, i have this code to parse all the articles:
while($article = $articles->fetch())
{
$date = strtotime($article['createdAt']);
$formatted_date = date("F Y",$date);
?>
<br />
<div class="news-content">
<div class="news-image">
<?php echo $article['title']; ?>
</div>
<div class="news-article">
<h3>
<span><?php $date = strtotime($article['createdAt']); echo /*date("F j",$date);*/ strftime('%e %B',$date) ?></span>
<br />
<?php echo $article['title']; ?>
</h3>
</div>
</div>
<?php
} //end while loop
?>
What i want to achieve: only the first 5 <div class="news-content">...</div> should be shown.
I know i have to do something with a for loop
but i do not know exactly how to use the for loop for this situation...
Can someone help me with that?
There are a lot of different ways to limit a loop. One possibility is to use a for loop instead of a while loop. for is often a good option if you want something to happen a specific number of times. Adding something else like fetch into the continuation condition will mean it happens up to a specific number of times.
for ($i = 0; $i < 5 && $article = $articles->fetch(); $i++) {
// output article
}
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Call Dialog</h3>
</div>
<!-- /.box-header -->
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tbody>
<tr>
<th>#</th>
<th>Callers</th>
<th>Expiration</th>
</tr>
<?php
$calls = array();
for ($i = 0; $i < 10; $i++) {
$template = "
<tr>
<td>". $i ."</td>
</tr>
";
echo $template;
}
?>
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
</div>
</div>
So the above code produces the following output:
Whenever I edit the code to add $i + 1 so that the end output is going to be a 10 what happens is the code breaks and the output is given:
This is the very first time I've ever encountered this problem. If you could figure out what I did wrong or somehow what is wrong with either my html, way of printing, etc. please do let me know. If you have any questions regarding the problem or is confused about something I'll do my best to clarify.
The error happens because you are summing the concatenation of 1 and whatever is after.
Very shortly, this is what you are doing:
echo 'a string' . $i + 1 . 'another string';
This will be the same as:
echo 0 + 1;
(Strings that don't start with a number will automatically be converted to 0. All other strings will be truncated to a number. '9a' will be converted to 9 before summing it while 'a9' will be 0)
How to solve this:
Use parenthesis:
Surround the operation in parenthesys will avoid it being concatenated.
A basic example:
echo 'Your string has ' . ($length + 1) . ' characters';
Use comas instead of concatenation:
This will avoid any problems and will very slightly speed your code.
This is a known micro-optimization.
Using the basic example:
echo 'Your string has ', $length + 1, ' characters';
Use whichever solution works best for you.
Please separate php and html. It's easy and will save a lot of time for you. It's working fine
<?php
$calls = array();
for ($i = 0; $i < 10; $i++) {
?>
<tr>
<td><?=$i+1 ?></td>
</tr>
<?php
}
?>
I want to set the css TOP property different after every 3 div's
for eg:
<div style="top:100px;">
</div>
<div style="top:100px;">
</div>
<div style="top:100px;">
</div>
**Now after this the next 3 div's shall have**
<div style="top:150px;">
</div>
<div style="top:150px;">
</div>
<div style="top:150px;">
</div>
**Now after this the next 3 div's shall have**
<div style="top:200px;">
</div>
<div style="top:200px;">
</div>
<div style="top:200px;">
</div>
**and so on**
I want to do this dynamically using php.
Using the for loop i can check if
$ij=0; if($ij%3==0) {
echo "";
}
else {
}
But not sure how to get desired result.
Any help is really appreciated.
Try this:
$top = 50;
$n = 30; // no. of divs
for($i = 0; $i < $n; $i++) {
if($i % 3==0) $top+=50;
echo "<div style=\"top:".$top."px;\">\n</div>";
}
I have a table in my database with 100 names, dates, id's, and other stuff. I want to be able to get a specific amount of rows echoed in a div, and then another specific amount in the next and so on.
I have been looking at foreach and break here, cause my googling kind of sent me there.. but maybe im looking at the wrong thing:
http://php.net/manual/en/control-structures.break.php
I just don't seem to get things right.
This is a school project and I know this is maybe a little bit vague, sorry about that.
Something like this:
<div id="div1"> echo row 1-7 </div>
<div id="div2"> echo row 7-19 </div>
<div id="div3"> echo row 20-44 </div>
and so on...
Could someone point me in the right direction?
You can do it directly in your query
Use at the end of your query this:
<div id="div1"> echo row 1-7 </div> use "LIMIT 0,6"
<div id="div2"> echo row 7-19 </div> use "LIMIT 7, 19"
<div id="div3"> echo row 20-44 </div> use "LIMIT 20,44"
if you have a numerically indexed array holding your rows you could do something like:
<div id="div1">
<?php for ($i = 1; $i <= 7; $i++) {
print $yourArray[Si]['some_field'];
} ?>
</div>
<div id="div2">
<?php for ($i = 8; $i <= 19; $i++) {
print $yourArray[Si]['some_field'];
} ?>
</div>
...
Something like this (not tested) might work:
$id=1;
$count=0;
$limit=20;
foreach($row as $r){
if ($count==0) echo "<div id='div$id'>";
echo $r;
if ($count++==$limit){
$count=0;
$id++;
echo "</div>\n";
};
}
if ($count!=0) echo "</div>";
I have a rather long code, so i think the best is I post it here with some additional comments as questions inside of it:
<div class="contentRow">
<h2>Title</h2>
<div id="frame">
<?php
//Get files
$sql->query("SELECT * FROM files WHERE job = 'true'");
$count = count($sql->get());
//No files to display
if ($count == "0"){
echo "<div class='box red'><p>No jobs found.</p></div>";
} else{
$sql->query("SELECT * FROM files WHERE job = 'true'");
$i=0;
foreach($sql->get() as $result){
$i++;
?>
<!-- Here I start printing the results and here the problems begin -->
<!-- My goal here: Print n .jobsContainer with exactly 4 .jobContainer inside -->
<?php if ($i % 4 == 0){ ?>
</div><!-- CLOSE jobsContainer -->
<?php } ?>
<?php if ($i % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
<!-- Print the .jobContainer -->
<div class="jobContainer">
content + table + form
</div>
<!-- My goal here: Print .jobSeperator after every 2nd .jobContainer -->
<?php if ($i % 2 == 0){ ?>
<div class="jobSeperator"> </div>
<?php } ?>
<!-- CLOSE loop -->
<?php } }?>
</div><!-- jobsFrame -->
<div class="clear"></div>
</div><!-- contentRow -->
The desired output should look like this:
My problem is following:
It seems that there are only 3 elements included in the first container, but after that there are 4. Therefore also the seperators are kinda messed up and it looks like this:
It seems that the problem is not applying to the .jobSeperator, as it does always get included after every 2th container, however it gets messed up too, because of the fact that in the first .jobsContainer there are just 3 sub containers.
I dont get why in the first .jobsContainer there are just 3 sub containers, but from than on there are, as desired, 4, but probably it is obvious and I just dont see it...
p.s. If I set the code to print 5 .jobContainer inside one .jobsContainer, than there are printed 4 in the first .jobsContainer and from than on 5 in every following .jobsContainer
First of all, you should not keep your SQL logic mixed with HTML.
Anyway. The idea is that you need to output a separator after each second item. The testcase i would look at would be like this.
1 2
---
3 4
***
5 6
---
7
And the code for writing it would be:
$i = 0;
foreach( /* some array */ as $data ){
$i++;
if ( ($i - 1) % 2 === 0 ){
if ( ($i - 1) % 4 === 0 ){
echo '<br />***<br />';
} else {
echo '<br />---<br />';
}
}
echo $i, ' ';
}
This would reproduce the structure above (never tested).
As for the separation of layout and rest of application logic, you might look in to this article. It will show a quite simple way, without a need of additional 3rd party libraries.
try to change these lines:
<?php if ($i % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
to
<?php if ( ($i-1) % 4 == 0 ){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
Now you open container at the begginning of 1st, 4th, 8th, 12th,... iteration, after this change you should open container before 1st, 5th, 9th, 13th,... iteration which should fix the problem. And you will get rid of ugly or. It seems this is the only bug, just try it.
I also suggest separating application logic and presentation.
I believe You're making one small mistake.
<!-- My goal here: Print n .jobsContainer with exactly 4 .jobContainer inside -->
<?php if ( ($i+1) % 4 == 0){ ?>
</div><!-- CLOSE jobsContainer -->
<?php } ?>
<?php if ( ($i+1) % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>
Use ($i+1) instead of $i in this bit.
If you inspect the generated HTML output, you'll see that when $i == 4, you first close the jobsContainer div before outputting the 4th jobContainer. You need to change the order of output to something like this:
EDIT: seems like you should split up the start of the first jobsContainer ($i == 1)
<div class="contentRow">
<h2>Title</h2>
<div id="frame">
<?php
$i = 0;
for($j = 0; $j < 12; $j++)
{
$i++;
?>
<?php if ($i == 1) { ?>
<div class="jobsContainer">
<?php } ?>
<div class="jobContainer">job</div>
<?php if ($i % 4 == 2) { ?>
<div class="jobSeperator"> </div>
<?php } ?>
<?php if ($i % 4 == 0) { ?>
</div>
<div class="jobsContainer">
<?php } ?>
<?php
}
?>
<?php if ($i > 0) { ?>
</div>
<?php } ?>
</div>
<div class="clear"></div>
</div>