<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
}
?>
Related
This table print at index page using jQuery.Ajax().it returns only true part at every time even condition is also false
$data['1']['0']['available']= 0 to 10;
echo "<div class='services'>
<div class='media-body'>
<h3 class='media-heading'>Check avability</h3>
<div class='table-responsive'>
<table class='table'>
<tbody>
<tr><th>Sr.no</th><th>Name</th><th>Status</th><th>Status</th><th>Action</th></tr>
<tr><td>1</td>
<td>Delux</td>";
echo ($data['1']['0']['available'] < 10) ? '<td>Available</td>': '<td>Not Available</td>' ;
echo"<td>".$data['1']['0']['available']."</td>
<td><button class='btn btn-success'>Book Now</button></td>"; echo "</tr>
</tbody>
</table>
</div>
</div>
</div>";
I don't know if you are using the keys as strings because they are defined as strings or you are doing it by mistake. If they are really strings then you should change them in my example as well.
echo intval($data[1][0]['available']) < 10
? "<td> Available </td>"
: "<td>Not Available</td>";
This is correct code and your logic is true and as per your code it should return 'Not Available'. I think this is the only way to write ternary operator.
I think the array is returning string value please confirm that array returns string or integer. If it returns string then first convert it into integer then try. I think after that it will works.
Make sure your value return in string format.
you try this code :
<?php
$data['1']['0']['available']=$any_numeric_value ;
echo "<div class='services'>
<div class='media-body'>
<h3 class='media-heading'>Check avability</h3>
<div class='table-responsive'>
<table class='table'>
<tbody>
<tr><th>Sr.no</th><th>Name</th><th>Status</th><th>Status</th><th>Action</th></tr>
<tr><td>1</td>
<td>Delux</td>".(int($data['1']['0']['available'] < 10) ? '<td>Available</td>': '<td>Not Available</td>' ).
"<td>".$data['1']['0']['available']."</td>
<td><button class='btn btn-success'>Book Now</button></td></tr>
</tbody>
</table>
</div>
</div>
</div>";
?>
I'm attempting to create a table using a loop. The number of columns is what matters, it should be 6-7, while the number of rows is irrelevant.
The problem here is that I need to create this from one array only, which has a set of image names which I need to display through the table.
This is the PHP:
if ($mode == 'skins')
{
$player_gender = ($player_data['playerGender'] == true) ? 'male' : 'female';
$skins_array = $samp->skin('small', false, $player_gender);
$index_counter = 0;
foreach ($skins_array as $skin_img)
{
$template->assign_block_vars('skinrow', array(
'IMAGE_PATH' => $root_path . $config['skins_path'] . '/Skin_' . $skin_img . '.png',
));
}
}
And this is the HTML:
<div class="container">
<table>
<!-- BEGIN skinrow -->
<tr>
<td><img src="{skinrow.IMAGE_PATH}" /></td>
</tr>
<!-- END skinrow -->
</table>
</div>
The template engine used in this case is from phpBB.
If I include the <tr> in the loop in the HTML, I get my results all going down (vertical) in one column and when I exclude the <tr> from the loop, the results all go aside in one row (horizontal).
So, I basically care for the number of columns only, I want those to be limited to 6-7.
I'm failing to see the logic on achieving this. Any help would be appreicated.
Here is an example array with the data I'm using: http://pastebin.com/uDMeBJw6
If the template engine is causing you trouble to understand the code, please let me know and I'll try to convert it to a pure PHP example.
MY phpBB skills are non-existant, but maybe this will do the trick:
Template:
<div class="container">
<table>
<tr>
<!-- BEGIN skinrow -->
{skinrow.NEW_TR} <!-- **EDITED** -->
<td><img src="{skinrow.IMAGE_PATH}" /> </td>
<!-- END skinrow -->
</tr>
</table>
</div>
PHP (loop only):
$counter = 0;
foreach ($skins_array as $skin_img)
{
$new_tr = ($counter && ($counter % 7 === 0)) ? '</tr><tr>' : ''; // **EDITED**
$template->assign_block_vars('skinrow', array(
'NEW_TR' => $new_tr,
'IMAGE_PATH' => $root_path . $config['skins_path'] . '/Skin_' . $skin_img . '.png',
));
}
Code is untested, this is just an idea.
... and it's probably cleaner to do for instead of dancing with foreach and $counter :)
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!
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>