PHP / HTML displaying only x amount of results depending on viewport - php

I'm using Bootstrap v3 and responsive design works perfectly everywhere besides in this example.
My issue is that when the page is scaled, it always shows 5 divs (as I would expect it to do with the code below), regardless on how many rows it takes up.
$array = array(1,2,3,4,5);
foreach($array as $row)
{
echo "<div style='display: inline-block;'>";
// HTML DIV Content here
echo "</div>";
}
What I want to do, that I can't figure out how is for it to count how many divs will fit in 1 or 2 rows. If 5 can fit in 1 row, then show 5 as 1 row. If 4 can fit in 1 row then show 8 as 2 rows. If 3 can fit in 1 row then show 6 as 2 rows.
So my array would probably contain the max that can be displayed (8 in this example).
I'm not sure if bootstrap can do this out of the box, or if PHP has this functionality. Or if I'm going to have to use jQuery to accomplish this.
Current Behavior:
Expected Behavior:

<table class='table table-responsive
'>
<tr>
<?php
$array = array(1,2,3,4,5);
foreach($array as $row)
{
echo "<td>";
// content here
echo "</td>";
}
?>
</tr>
</table>
Use table responsive instead of div classes and it will render on one line.
Apologies for poor format or errors on my I phone it's never the best experience.
Or if you want to control by size of div it gets harder. You can get same number in each row by echoing bootstrap div classes I.e col-md-3 for example. If you want it to know the widths and your body width is known floats might work.

Related

How to style the output of an echo to display a grid?

My team and I have made a database in php my admin for a restaurant, and I'm currently working on the customer dashboard. Im using for each loops to display complete orders in one of the dashboard tabs, and have the code working, but right now it just outputs regular black text. I was wondering how to style it to output the rows as a grid, similar to bootstrap grids.
I've tried to just add containers with rows and columns to the foreach echo itself, but its just not working as I thought it would.
<div id="CurrentOrders" class="tabcontent" style="margin-left: 24px">
<!-- This information will be pulled from the Orders table in the DB -->
<h3>Current Orders</h3>
<p>
<div class="container">
<?php
foreach ($orderno as $order) {
$n = $order['OrderNo'];
$menunamequery = "SELECT * FROM OrderItem WHERE OrderNo = '{$n}'";
$currentorders = getRows($menunamequery);
foreach ($currentorders as $currentorder) {
echo "Order Number -"." ".$currentorder['OrderNo']." , "."Order -"." ".$currentorder['MenuName']." , "."Quantity -"." ".$currentorder['Quantity']."<br>";
}
}
?> </div>
</p>
</div>
The expected result is for these rows im outputting to have some sort of grid layout, the actual result is just plaintext currently.
Sorry if this is a bad question, my team and I just learned php this semester and are hoping to continue getting better at it. Any help would be appreciated.
You can simply output HTML from PHP:
echo '<span style="color: red">'.$currentorder['MenuName'].'</span>';
However, it is advised that you sanitize your output, so nobody can "create HTML" by putting tags in the database;
echo '<span style="color: red">'.htmlspecialchars($currentorder['MenuName']).'</span>';
This does exactly what it says; makes HTML entities from special characters. For example, > will be printed as >, which the browser will safely render as >, instead of trying to interpret it as an HTML element closing bracket.
Alternatively, you can simply write HTML directly if you wish, by closing and opening the PHP tags:
// PHP Code
?>
<span class="some-class"><?=htmlspecialchars($currentorder['MenuName'])?></span>
<?php
// More PHP Code
You may also want to look into templating engines to make it easier for you, although it depends on the project if it's worth it for you to look into that, since there is a little bit of a learning curve to it.

How to display data in two columns with PHP "Labels print"?

What should I do so that I can not do the following? I want to print out the results as an address label.
1 2
3 4
5 6
7 8
... ...
Sample view
Here's a solution echoing html table tags and variables, you could also use css. Some will say css is better, but this sort of tabular arrnagement of data is exactly the reason table tags were designed. $ variable fields will automatically be identified as variables and parsed to the undsrlying inofrmation in memory, so you do not need to concatenate the fields and strings, but it makes the code more precise and readable. You could just embed the variables in the string if you wish.
<?php
$c1r1="col1row1";
$c2r1="col2row1";
$c1r2="col1row2";
$c2r2="col2row2";
echo "
<table border \"=2\">
<tr><td>". $c1r1."</td><td>".$c2r1."</td></tr>
<tr> <td>".$c1r2."</td> <td>".$c2r2."</td></tr>
</table>";
?>
Reference to official w3 docs on tables

How to count an array content and assign number position with php

am working on a website project in which i fetch array from my database and use foreach statement to output its contents.
I want use php script to count how many items are in the array and assign number position to each item so that if the number position of the first item is an odd number i will apply css float:left style to it but if its even number it will float:right
any help please??.
thank you.
Use the modulus operand:
<?php foreach ( $array as $index => $item ): ?>
<div class="<?php echo $index % 2 ? 'even' : 'odd'; ?>"></div>
<?php endforeach; ?>
Then just use those class names in your CSS:
.odd { float: left }
.even { float: right }
CSS3 has nice features (.list:nth-child(odd) {float:left;} .list:nth-child(even) {float:right;} ), but beware that this will not work on many browsers (ie8 and below, older firefox, etc) .. at least every user with WinXP+IE will see just a normal list without the different colors.
jQuery (also noted here) can also select with $('.xy:odd').css({'float':'left'}); , but if you are not using jQuery in your project its just a big (90kb jQuery library) overhead.
Performance is also better if going with php.
So you better go with php and the modulo operand (if $count % 2), see Joseph Silber's answer here.
I think you can also use jquery with :odd selector.
You can do it with css only:
.list:nth-child(odd) {float:left;}
.list:nth-child(even) {float:right;}

Aligning text inside an <option> tag

I have a select tag with a lot of options. Every option has a name and a date which both should be printed in every <option>.
I want to align the list like this:
Name Date
Name Date
Name Date
Since every name has different length, I wrote this code:
//Start of the option text//
//Always print 20 characters, even if the name is longer//
print ">".substr($eventsArr[$j]->GetName(),0 ,20);
//If the name is shorter then 20 chars//
if(strlen($eventsArr[$j]->GetName()) < 20)
{
//Add missing chars (20 - the length of the string) with spaces//
for($t = 0; $t < 20 - (strlen($eventsArr[$j]->GetName())); $t++)
{
print "&nbsp";
}
}
print "" .$newDate."</option>\n"; //Then print the date//
I'm getting the correct amount of spaces completed. But as you can see, the alignment is not 100%:
I'm guessing its because every letter has a different width in pixels. So... Is there any way of doing this kind of alignment ? Thanks.
Just use a Monospaced font for this. It's what they were designed for.
Option elements were not meant to be formatted that way. Using a monospace font would be the way to achieve alignment, but that would make the text look ugly, and monospace fonts are less readable, too. Moreover, not all browsers honor font family settings for option elements; for example, IE 9 does not.
The workaround, or alternative approach, is to use a set of radio buttons instead of a select element. Then you can use a table:
<table>
<tr><td><input type=radio id=a1> <label for=a1>Name</label>
<td>Date
...
</table>
This handles the alignment, and this also lets you specify the font used for the labels in a manner that works across browsers.
If there is a large number of alternatives, you might consider setting a height and vertical scrolling for the table. But it might be better to let users just scroll down the page as needed, instead of having two levels of scrolling.

Array contents not displaying well, when placed between certain lines of the code

Whenever I move the codeblock that generates courses with a grade of "F", it does not not echo out the courses that meets the criteria. But when I move it to beginning of the main div header of the script it displays well.. What could the problem be? I actually want it to be at the end of the script for the sake of printing out. I've also checked the source code and the parameters I was looking for wasn't there.
Pastebin of full code
echo "<table bgcolor = red >";
echo "<tr align= \"center\">";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<th>"."Failed Courses : "."</th>";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo"<td>". $row8['course_code']."</td>;
}
}
echo "</tr>\n";
echo "</table>";
There are inconsistencies between your provided example here and the pastebin code you linked to. Which version are you using?
If it's the pastebin version you may be having issues with this line:
echo "<th>"."Failed Courses";
You are missing the </th> tag which could be messing with your output.
It seems like it is going to be a long bit of code so I'm posting it here, I don't intend for this to be an answer to your problem as I can't get it to do what you're saying.
PHP allows for information to be set outside of the PHP tags, and when that happens it is treated as regular HTML.
E.G.
<?php
If(2==2){
?><b>HELLO!</b><?php
}
?>
And
<?php
if(2==2){
echo "<b>HELLO!</b>";
}
?>
Will both result in <b>HELLO!</b> to be output to the screen. This is useful for things that might require a lot of HTML in more than one block, as well as HTML that may require extensive style definitions or other places where double quotes will be needed, for example
<div id="list_row[$i]" class="something something2 something3"> would have to be escaped as
echo <div id=\"list_row[$i]\" class=\"something something2 something3\"> whereas it could just be put more or less intact in php using the above mentioned fact. Now, there would still need to be an echo statement for the $i portion, as I don't think PHP processes the text, but I've never tried it so I can't be sure.
As for your HTML,
<th> tags are meant to be a header, aka in the top part of a table. <td> are meant to be table-data cells.
Seems like you're making a lot of unnecessary calls to the same MySQL tables, and I'm hoping I can condense it down a bit.
Why are you doing this?
echo "<th>"."Failed Courses : "."</th>";
do
echo "<th>"."Failed Courses : </th>";
and also before you do a while loop try to see what your results looks like.
$row8 = mysql_fetch_assoc($query8)
then print_r($row8);
post your results.
Also the table structure should be
<table>
<th></th>
<tr>
<td></td>
</tr>
</table>

Categories