I want to make a dynamic grid using php and bootstrap.
For example
Case 1 => If i have total 6 records then row 1 contain col-md-4 grids and row 2 contain col-md-4 grids
Case 2 => If i have total 5 records then row 1 contain col-md-4 grids and row 2 contain col-md-6 grids
Case 3 => If i have total 4 records then row 1 contain col-md-4 grids and row 2 contain col-md-12 grids
How to do that i have no idea, any solution?
<?php foreach($Banners as $i=>$DATA){
if($count%3==1){
echo "<div class='row'>";
$lgclass = "col-lg-4";
}
if($count%4==0){
$lgclass = "col-lg-6";
}
if($count%5==0){
$lgclass = "col-lg-6";
}
?>
<div class="<?php echo $lgclass;?> col-xs-12 col-sm-12" <?php echo $count;?>>
<h2 class="section-title" > </h2>
<a href="http://mymegarealty.net/index.php?option=com_jointeam">
<div class="full-width">
<img src="<?php echo $Imageurl;?>" class="img-responsive" style="height:250px;;display:unset;">
</div>
</a>
</div>
<?php
if($count%3==0){
echo "</div>";
}
$count++;} ?>
it would be better a recursive solution, if you want you can do it. I wrote you at the beginning the logic if I follow an example overturned on your needs. my logic was the procedural one.
<?php
/*
logic
while myElements !=0
if myElements > 2
dispay row whith 3 elements
myElements remove firs 3
else if myElements == 2
dispay row whith 2 elements
myElements remove firs 2
else if myElements == 1
dispay row whith 1 elements
myElements remove firs 1
end while
*/
// init my data
$data=[];
$element=5;
for($i=0;$i<$element;$i++){array_push($data,$i);}
var_dump($data);
// do code
function displayElement($data, $classAdd){
echo "<div class='".$classAdd."'>";
echo $data; // your cell
echo "</div>";
}
while (sizeof($data)!=0){
if (sizeof($data)>2){
echo "<div class='row'>";
displayElement($data[0],"col-lg-4");
displayElement($data[1],"col-lg-4");
displayElement($data[2],"col-lg-4");
echo "</div>";
$data=array_slice($data,3);
}elseif (sizeof($data)==2){
echo "<div class='row'>";
displayElement($data[0],"col-lg-6");
displayElement($data[1],"col-lg-6");
echo "</div>";
$data= array_slice($data,2);
}elseif (sizeof($data)==1){
echo "<div class='row'>";
displayElement($data[0],"col-lg-12");
echo "</div>";
$data= array_slice($data,1);
}
}
You should be able to simply achieve this with flex. But with bootstrap try to use recursion: small example below
<?php
$elements = [1, 2, 3, 4];
function displ(&$el, $out = "") {
if (count($el) === 0) {
return $out;
} elseif (count($el) > 3) {
$i = 0;
foreach ($el as $k=>$v) {
$out .= "<div class=\"col-md-4\">" . $v . "</div>";
unset($el[$k]);
$i++;
if ($i === 3) {
return displ($el, $out);
}
}
return displ($el, $out);
} elseif (count($el) < 3) {
foreach ($el as $k=>$v) {
$out .= "<div class=\"col-md-" . 12 / count($el) . "\">" . $v . "</div>";
unset($el[$k]);
}
return $out;
}
}
echo displ($elements);
Related
My content of the subcategories is getting out of the div somehow.
The first one stays on there but the second, third etc is getting out from it.
Any solutions to it?
<?php
...
$stmt = $dbh->query('SELECT parent.subcat_id, parent.subcat_name, child.subsubcat_name, child.subcat_id, child.cat_id FROM subcategories parent JOIN subsubcategories child ON child.cat_id = parent.cat_id');
$lastcat = 0;
$stmt->execute();
$row2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($row2 as $row) {
if($lastcat != $row['cat_id']){
$lastcat = $row['cat_id'];
echo '<div class="panel panel-default">
<div class="panel-heading">';
echo $row['subcat_name'];
echo '</div><div class="panel-body">';
}
echo $row['subsubcat_name'];
echo "</div></div>";
}
?>
This should be rewritten like:
$close_previous = false; // special flag
foreach ($row2 as $row) {
if ($lastcat != $row['cat_id']) {
$lastcat = $row['cat_id'];
// check whether you need to close divs from previous block
if ($close_previous) {
echo '</div>'; // close .panel-body
echo '</div>'; // close .panel-default
} else {
// for the first time you don't need
// to close divs, so here we skip it
$close_previous = true;
}
echo '<div class="panel panel-default"><div class="panel-heading">';
echo $row['subcat_name'];
echo '</div><div class="panel-body">';
}
echo $row['subsubcat_name'];
// add link
echo 'Click';
}
// explicitly close last divs block
echo '</div>'; // close .panel-body
echo '</div>'; // close .panel-default
Also it's a good practice to check your generated html-markup to see what is wrong.
i want my script to add a closing div tag after every fourth entrie of the db. i tried something like this:
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i=0;
i++;
if ($i == 4){
echo "</div>";}
}
?>
would be great if you can help me here. thx
1) Get names of all cities
2) Initiate an indexing variable
3) Ieterate through the loop
4) Open <div class="row"> when $i==0 (Very First time) OR $i%4==0 (When fifth entry is to be printed). (Remember you are using 0 based indexing i.e. initializing $i=0).
5) Close the div tag for <div class="row"> when fourth city name has been printed i.e. $i%3==0 (Remember you are using 0 based indexing i.e. initializing $i=0).
Here's the code:
<?php
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
if ($i == 0 || $i%4==0){ // <div class="row"> opens on first entry and every fifth entry
echo "<div class=\"row\">";
}
echo "<div class=\"col-sm-4 col-md-3\">";
echo "<h3>".$zeile['name']."</h3>";
/*
Rest of your code
*/
echo "</div>";
$i++;
if ($i % 3 == 0){
echo "</div>"; // <div class="row"> closes here on every fourth entry
}
}
?>
Try this code,
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i++;
if ($i == 4){
echo "</div>";
$i = 0;
}
}
?>
Just beginning PHP to bear with me.
Results I'm trying to achiever:
I have a table of YouTube URL's and MetaData.
Trying to build this:
<div class="slide">
<iframe></iframe>
<iframe></iframe>
</div>
<div class="slide">
<iframe></iframe>
<iframe></iframe>
</div>
Two videos per slide, then I'm going to paginate through results using Deck.js.
I suspect I'm going about this completely the wrong way, not that experienced at programmin g logic;
while($data = mysql_fetch_array($result)) {
for ($counter = 1; $counter<=2; $counter++) {
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
/* If Video 1, increment counter for 2nd video */
if ($counter == 1) {
$counter++;
}
/* If Video 2, close div and reset counter */
else if ($counter == 2) {
echo "</div>";
$counter = 1;
}
/* If error break out */
else {
echo "</div>";
break;
}
}
}
Basically trying to nest loops to keep track of how many videos per div and start a new one when a div has two.
I've tried a few different ways, this being the latest. Results in:
<div class="slide">
<iframe></iframe>
<div class="slide>
<iframe></iframe>
Hit the blank wall now, not sure what to try next. Willing to use/learn any method to accomplish the results, just not sure where to go at this point.
Cheers.
You could remove the second loop all together using the % operator (modulus). The idea is that a % b === 0 then the number a was evenly divisible by b. Using this, you can easily check for even or odd or every Nth row.
$k = 1;
echo "<div class=\"slide\">";
while($data = mysql_fetch_array($result)) { // you should really change to mysqli or PDO
if($k % 3 === 0){
echo "</div>";
echo "<div class=\"slide\">";
}
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
$k++;
}
echo "</div>";
Put the echo <div> before the for loop (still inside the while loop) and the </div> after the for loop
In your while loop you're retrieving just one row, but then you're iterating over it twice with a nested loop. Do away with the inner loop and just use a flip-flop variable to track left and right. I think this will do what you want:
$left=true; // track whether we're emitting HTML for left or right video
while($data = mysql_fetch_array($result)) {
if ($left) {
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
}
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
if (!$left) {
echo "</div>";
}
$left = !$left; // invert $left to indicate we're emitting the right iFrame
}
// end of loop. If we had an odd number of
// videos, tidy up the HTML
if (!$left) {
echo "</div>";
}
PHPFiddle
<?php
$x = 10;
$counter = 0;
while($x > 0)
{
if($counter != 0 && $counter % 2 == 0)
{
echo "ENDOFSLIDE</br>";
}
if($counter == 0 || $counter % 2 == 0)
{
echo "SLIDE</br>";
}
echo "iframe => $x </br>";
$x--;
$counter++;
}
echo "ENDOFSLIDE";
?>
It won't work because the for loop is inside the fetch loop for the SQL data. The second iteration of the for loop does not have a new SQL row. A better solution would be to capture the common column that identifies the two videos (the title) and generate a new div whenever that value changes. Try something like this, which will work for any number of SQL rows with the same title. This will also give proper results if the SQL query returns no rows and will handle the potential of a title with only one URL - which could get ugly if you merely flip-flop and end up with URLs on the wrong title. Of course, as in your current solution, your SQL query must ORDER BY VIDEO_TITLE so the rows are adjacent. I didn't run it, but should be close.
$lastTitle = "";
$n = 0; //count SQL rows processed
while($data = mysql_fetch_array($result)) {
// See if the title changed from last
if( $data['VIDEO_TITLE'] != $lastTitle ) {
// if we processed any rows, must end the current div before starting a new one
if( $n > 0 )
echo "</div>";
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
//save the title as last title
$lastTitle = $data['VIDEO_TITLE'];
}
$n++; //count the SQL row
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
}
if( $n > 0 )
echo "</div>";
I have a section in my footer from a script, that I am trying to customize and that displays the website categories.
Currently the results were limited to 9 results per column and continuing onto x amounts of columns. I need to be able to stop at 2 columns.
Here is the code I have
<div class="left">
<h3 style="padding-top:15px;"><?php echo BROWSE_CATEGORIES; ?></h3>
<?php $footer_category= $this->home_model->get_category(); ?>
<?php if($footer_category) { $ftr_cnt=1; ?>
<ul>
<?php foreach($footer_category as $cat) {
echo '<li>'.anchor('search/category/'.$cat->project_category_id,substr($cat->project_category_name,0,30)).'</li>';
if($ftr_cnt>9) { $ftr_cnt=1; echo "</ul><ul>"; }
$ftr_cnt++;
}
} ?>
</ul>
</div>
Use a break when you've reached 9 * 2, where 2 is the amount of columns you want.
$ftr_cnt = 1;
foreach($footer_category as $cat)
{
echo '<li>'.anchor('search/category/'.$cat->project_category_id,substr($cat->project_category_name,0,30)).'</li>';
if($ftr_cnt == 9)
{
echo "</ul><ul>";
}
$ftr_cnt++;
if($ftr_cnt >= (9 * 2))
{
echo "</ul>";
break;
}
}
Every third element extracted from the database will output in the large box, while every other element will output in the small box. What I need it to do is exclude the third element when the small box is outputted. Any ideas on how to accomplish this?
while($array = mysql_fetch_assoc($result)) {
if($i % 3 == 0) {
// large box
echo '<div class="box" style="width: 692px; height: 218px">' . $array['feedName'] . '</div>';
}
// small box
echo '<div class="box" style="width: 246px; height: 218px">' . $array['feedName'] . "<br></div>";
// exclude the third element
$i++;
}
}
If I understand what you want correctly (each third item is in the large box and kept out of the small box), you just use an else clause in your if.
while($array = mysql_fetch_assoc($result)) {
if($i % 3 == 0) {
// large box
echo '<div class="box" style="width: 692px; height: 218px">' . $array['feedName'] . '</div>';
}
else {
// small box
echo '<div class="box" style="width: 246px; height: 218px">' . $array['feedName'] . "<br></div>";
}
$i++;
}
divide by 6 and get the remainder (%)
if (remainder == 0 or 3) {
large box
} else if (remainder == 1 or 5) {
small box
}