Displaying each two array items in one loop item with right placement - php

I am trying to do something fit with the design, so i have to show 2 item in 1 loop. Also, according to this design, should be line break in each 4 loop. Line breaks applying automatically, but its fixed 4. So, in 4 loop it shows actually 8 items. However, the point I'm stuck on is; items are not displaying in the placement I want. You can see the placement I want to do with more detail in the picture.
Red Numbers = Current placement
Green Numbers = Placement it should be
Yellow boxes = For loop items ( 8 items )
Black boxes = Items in array (16 items )
.
ul,li {
padding:0;
margin:0;
list-style:none;
}
li {
width:30px;
}
.gen {
width: 140px
}
li {
display: inline-block;
}
.ic {
width: 100%
}
<ul class="gen">
<li>
<ul>
<li class="ic">a</li>
<li class="ic">b</li>
</ul>
</li>
<li>
<ul>
<li class="ic">c</li>
<li class="ic">d</li>
</ul>
</li>
<li>
<ul>
<li class="ic">e</li>
<li class="ic">f</li>
</ul>
</li>
<li>
<ul>
<li class="ic">g</li>
<li class="ic">h</li>
</ul>
</li>
<li>
<ul>
<li class="ic">i</li>
<li class="ic">j</li>
</ul>
</li>
<li>
<ul>
<li class="ic">k</li>
<li class="ic">l</li>
</ul>
</li>
<li>
<ul>
<li class="ic">m</li>
<li class="ic">n</li>
</ul>
</li>
<li>
<ul>
<li class="ic">o</li>
<li class="ic">p</li>
</ul>
</li>
</ul>
As you can see, items are not placing alphabetically from left to right.
Here is the loop i used :
// 16 items
$items = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p');
$half = count($items) / 2;
$k = 0;
echo '<ul class="gen">';
for($i = 0; $i<$half; $i++) {
echo '<li>';
echo '<ul>';
echo '<li class="ic">'.$items[$k].'</li>';
echo '<li class="ic">'.$items[$k+1].'</li>';
echo '</ul>';
echo '</li>';
$k = $k+2;
}
echo '</ul>';

I hope this helps. You can run this code and see the output.
// chaganged the array to match your picture.
$items = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
//used to format the output
echo "<pre>";
for ( $i = 0; $i<count($items) ; $i++ ) {
echo "loop start \n";
// access 1st item
echo $items[$i]."\n";
// based on your example, it seems that you need the item that is 4 places after the first item of the loop
echo $items[$i+4]."\n";
echo "loop end\n";
// we can use mod to check if we have reached the 4th element (index 3), we use $i+1 to check the second element of the loop
if ( ( $i+1 )%4 == 0 ) {
echo "\n---new line---\n";
$i = $i + 4;
}
echo "\n";
}
echo "</pre>";

Assuming that you will take care of the formatting, your code could be changed to this:
$items = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p');
for($first = 0; $first < count($items); $first += 8) {
for($add = 0; $add < 4; $add++) {
echo $items[$first + $add]; //first item
echo $items[$first + $add + 4]; //second item
}
}

May I see your HTML code? Here's a suggestion, display your results like this (run code snippet and use my css and php loop);
#wrapper{
background-color:rgba(0,0,0,0.6);
padding:10px;
}
.result{
display:inline-block;
padding:7px;
border:3px solid #FEBF01;
width:120px;
}
.result-child{
display:block;
padding:10px 10px;
background-color:rgba(0,0,0,0.8);
color:rgba(50,255,100,1);
font-size:2em;
text-align:right;
}
.result .result-child:first-of-type{
margin-bottom:10px;
}
<div id="wrapper">
<div class="result">
<div class="result-child">1</div>
<div class="result-child">1</div>
</div>
<div class="result">
<div class="result-child">2</div>
<div class="result-child">2</div>
</div>
</div>
Loop code:
for($i = 0; $i<$half; $i++) {
echo "<div class='result'>";
echo "<div class='result-child'>".$items[$k]."</div>"; //first item
echo "<div class='result-child'>".$items[$k+1]."</div>"; //second item
echo "</div>";
$k = $k+2;
}

Related

How to get dropdown dynamically with ul li inside ul using php?

I have a ul li with parentid . I want all ul li with parentid but I'm getting multiple loops. The first steps and last are under same parentid.
<ul class="section-list">
<?php
if(count($outArrResults)>0){
for($i=0; $i<count($outArrResults); $i++){
?>
<li class="o-view">
<a href='javascript:void(0);'>
<h4 class="tit-section xsm">
<?php echo isset($outArrResults[$i]['cName']) ?$outArrResults[$i]['cName'] : '';?>
</h4>
</a>
<ul>
<?php
for($s=0; $s<count($outArrResults); $s++){
$contentSlugUrl = isset($outArrResults[$s]['conSName']) ? $outArrResults[$s]['conSName']:'';
$contentMenuTitle = isset($outArrResults[$s]['conMTitle']) ? $outArrResults[$s]['conMTitle']:'';
if($contentMenuTitle!=''){
?>
<li>
<a href='<?php echo $config['LIVE_URL'].'courses/'.$pAction1.'/'.$pAction2.'/'.$contentSlugUrl;?>'>
<span><?php echo $contentMenuTitle;?></span>
</a>
</li>
<?php
}
}
?>
</ul>
</li>
<?php
}
} else {
echo "No Results";
}
?>

Break UL tag after 'n' times in while-loop

I'm currently in developing module shop for the company I work for and there is a little problem. While extracting records from the table, I want for every third data record to close HTML "UL" tag.
This is what I currently have:
<?php
$selektKat = "SELECT * FROM `proizvodi` WHERE `kategorija` = '$kat'"; //don't worry about SQLi; I will fix it
$result = mysqli_query($con, $selektKat) or die(mysqli_error());
// Line where the loop starts
<?php
while ($row = mysqli_fetch_array($result)) { ?>
<ul class="products-grid clearfix" style="margin-right: 5px; margin-left: 20px;">
<li class="item" style="min-height: 339px">
<a id="product-image-42321" href="proizvod.php" title="naziv" class="product-image">
<img src="http://static.511tactical.com/mag/media/catalog/product/cache/1/small_image/220x/9df78eab33525d08d6e5fb8d27136e95/5/3/53212_716_Alternate1.jpg" width="220" alt="<?php echo $row['naziv'] ?>" />
</a>
<ul class="swatches clearfix">
<li id="swatch-228" class="swatch product-42321">
<a href="proizvod.php" title="<?php echo $row['naziv']; ?>">
<img src="<?php echo __DIR__ . '/images/' . $row['slika'] . '.jpg'; ?>" />
</a>
</li>
</ul>
<div class="price-box">
<span class="label" id="configurable-price-from-42321">
<span class="configurable-price-from-label">
</span>
</span>
<div class="product-name"><a id="product-name-140981" href="proizvod.php"><?php echo $row['naziv']; ?></a></div>
<span class="regular-price" id="product-price-42321">
<span class="price"><?php echo $row['cijena']; ?><sup>KM</sup>
</span>
</span>
</div>
<div class="actions">
</div>
</li>
<?php
}
?>
</ul> // This has to be closed in loop after every 3 records
</div>
Picture:
Cheers.
First, I provide simple PHP script that can be addapted to your needs. In this script open and end tags are handled properly.
I added also $break_after variable - you can set here any positive value you want - in your case it's 3 because you want to do the action after each 3rd element.
First method (it assumes you can get number of data elements before loop)
<?php
$data = array(1,2,3,4,5);
$break_after = 3;
$counter = 0;
$totalNumber = count($data);
foreach ($data as $item) {
if ($counter % $break_after == 0) {
echo '<ul>';
}
echo '<li>'.$item.'</li>';
if ($counter % $break_after == ($break_after-1) || $counter == $totalNumber-1) {
echo '</ul>';
}
++$counter;
}
Second method (it assumes you cannot get number of data elements before loop)
<?php
$data = array(1,2,3,4,5);
$break_after = 3;
$counter = 0;
foreach ($data as $item) {
if ($counter % $break_after == 0) {
echo '<ul>';
}
echo '<li>'.$item.'</li>';
if ($counter % $break_after == ($break_after-1)) {
echo '</ul>';
}
++$counter;
}
if ((($counter-1) % $break_after) != ($break_after-1)) {
echo '</ul>';
}
Regarding your question, you also need to remember to start your <ul> each 3rd record (not just closing it) but also make sure to close it after your loop. In your case you can use second method because you don't know number of elements (in fact you can get them using mysqli_num_rows function and then you could also use method 1). For your case your code should probably look like this:
<?php
$selektKat = "SELECT * FROM `proizvodi` WHERE `kategorija` = '$kat'"; //don't worry about SQLi; I will fix it
$result = mysqli_query($con, $selektKat) or die(mysqli_error());
// Line where the loop starts
<?php
$counter = 0;
$break_after = 3;
while ($row = mysqli_fetch_array($result)) {
if ($counter % $break_after == 0) {
?>
<ul class="products-grid clearfix" style="margin-right: 5px; margin-left: 20px;">
<?php } ?>
<li class="item" style="min-height: 339px">
<a id="product-image-42321" href="proizvod.php" title="naziv" class="product-image">
<img src="http://static.511tactical.com/mag/media/catalog/product/cache/1/small_image/220x/9df78eab33525d08d6e5fb8d27136e95/5/3/53212_716_Alternate1.jpg" width="220" alt="<?php echo $row['naziv'] ?>" />
</a>
<ul class="swatches clearfix">
<li id="swatch-228" class="swatch product-42321">
<a href="proizvod.php" title="<?php echo $row['naziv']; ?>">
<img src="<?php echo __DIR__ . '/images/' . $row['slika'] . '.jpg'; ?>" />
</a>
</li>
</ul>
<div class="price-box">
<span class="label" id="configurable-price-from-42321">
<span class="configurable-price-from-label">
</span>
</span>
<div class="product-name"><a id="product-name-140981" href="proizvod.php"><?php echo $row['naziv']; ?></a></div>
<span class="regular-price" id="product-price-42321">
<span class="price"><?php echo $row['cijena']; ?><sup>KM</sup>
</span>
</span>
</div>
<div class="actions">
</div>
</li>
<?php
if ($counter % $break_after == ($break_after - 1)) {
echo '</ul>';
}
++$counter;
}
if (($counter-1) % $break_after != ($break_after - 1)) { // make sure there will be closing </ul>
echo '</ul>';
}
?>
</div>
<?php
//Our images
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14);
//Count array for last ul
$count_array = count($array);
//Counter
$count = 0;
foreach ($array as $key => $value) {
$count++;
//Begin
if ($count % 3 == 1) {
echo '<ul>';
}
//Output data
echo '<li>' . $value . '</li>';
//Close ul
if ($count % 3 == 0 || $count_array == $count) {
echo '</ul>';
}
}
?>
Output:
<ul><li>1</li><li>2</li><li>3</li></ul><ul><li>4</li><li>5</li><li>6</li></ul><ul><li>7</li><li>8</li><li>9</li></ul><ul><li>10</li><li>11</li><li>12</li></ul><ul><li>13</li><li>14</li></ul>
1234567891011121314
You specifically ask for opening and closing <ul> tags, but may I suggest an alternative, using only css, which can be easily converted into a responsive solution?
To start with, just put your items in a div instead of an list item (omitting the <ul> altogether):
<div class="product-grid">
<?php while ( $row = mysqli_fetch_array( $result ) ): ?>
<div class="item"> ... </div>
<?php endwhile; ?>
</div>
Now just use plain old css to float the grid and add margins in between the columns. Then using the nth-of-type pseudo-selector we can clear the margin on the nth element, depending on how many items you want in a row.
Always-3-column-grid example (Fiddle)
.product-grid { width: 100%; }
.product-grid .item { float: left; width: 32%; margin-left: 2%; margin-bottom: 2%; }
.product-grid .item:nth-of-type(3n+1) { margin-left: 0; }
Now the items are placed nicely in a grid, and it doesn't matter at all how wide the container is.
Fiddle note: the overflow: hidden is a kind of clearfix to show the border. It also works without it.
Now if you want, for instance, add breakpoints to show a 2-column grid instead of a 3-column grid on smaller devices, just change the width of the item and the counter of the pseudo selector. I've put the 3-grid counter in a media query as well, so you don't have to overwrite that one.
3-to-2-column-grid example (Fiddle)
Don't forget to resize the example panel to see the responsive result ;)
.product-grid { width: 100%; }
.product-grid .item { float: left; width: 32%; margin-left: 2%; margin-bottom: 2%; }
/* 3-column */
#media (min-width: 501px) {
.product-grid .item:nth-of-type(3n+1) { margin-left: 0; }
}
/* 2-column. Don't forget to alter the item width accordingly */
#media (max-width: 500px) {
.product-grid .item { width: 49%; }
.product-grid .item:nth-of-type(2n+1) { margin-left: 0; }
}
And here you go! Your fully responsive 3-to-2-column grid :) Just alter the breakpoints to your needs, and the width and margin sizes (just to make sure that in a 3-column version 3*columnWidth + 2*marginWidth adds up to 100%, and 2*columnWidth + marginWidth for the 2-column version).
$i=0;
while ($row = mysqli_fetch_array($result)) {
// Your code
if($i % 5 == 0){ // Break </ul> tag here}
// Rest of code
$i++;
} // End while loop
the % is modulus, it returns the remainder after a division is done, so
5 % 5 = 0
9 % 5 = 4
Add a varaible, let's say $i in you while that you increment at each iteration.
And when it is a multiple of 3, just close your ul.
while ($row = mysqli_fetch_array($result)) {
// YOUR CODE HERE
$count++;
if ($count % 3 == 0) echo '</ul>';
}
For the record the %operator will give you the remainder of the euclidean division.

margin list after 5 li items with php

I hope topic title will be okey, doesnt really find good idea for naming topic. What i want is to produce that my UL list will get some class or margin after a few LI in list.
Something like that
<ul>
<li></li>
<li></li>
<li></li>
</ul>
<ul class="1">
<li></li>
<li></li>
<li></li>
</ul>
<ul class="2">
<li></li>
<li></li>
<li></li>
</ul>
Now what i have in code is something like that, but offcourse doesnt work...:
$datas = array('a','b','c','d','e','f','g','h');
$countData = count($datas);
for($i = 0; $i < $countData; $i++){
echo '<ul>';
$j = 0;
foreach($datas as $data){
$j++;
if($j < 3){
echo '<li>'.$data.'</li>';
}
}
echo '</ul>';
}
Guess you can try this:
$datas = array('a','b','c','d','e','f','g','h');
echo "<ul>\n";
$class = 0;
foreach ($datas as $i => $data) {
echo "<li>$data</li>\n";
if (($i+1) % 3 == 0)
echo "</ul>\n<ul class=".++$class.">\n";
}
echo "</ul>";
It produces:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<ul class=1>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
<ul class=2>
<li>g</li>
<li>h</li>
</ul>

Got all the same ID's from api, what can i do to make them count itself?

I'm getting all the data from GT api (xml) from link: http://api.gametracker.rs/demo/xml/server_info/217.26.212.10:20021/
Especially for players list, as you can see, all the PID's are 0, i'm displaying them in table with following code:
<?php
foreach( $players as $player ) {
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
?>
What I can do to make pid count itself for as many as found players in xml file?
$i = 0; //Counter variable
foreach( $players as $player ) {
$i++; //Increment the counter for each iteration
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
echo 'There were ' . $i . ' players in total';
All you need to do is have a variable for that:
$i = 1;
foreach ($players as $player)
{
echo "<ul>
<li id=\"number-$i\">$i.</li>
<li id=\"nickname-$i\">$player->name</li>
<li id=\"score-$i\">$player->score</li>
<li id=\"time-$i\">$player->time</li>
</ul>";
$i++;
}

Tree Structure using ul,li format in php

Code for Tree structure using ul, li in php...
*In the output i am missing ul or in some place.*
I want this for checkboxes when we click the parent check box their childs must be selected and sub childs too.
<?php
$groups = array(
array('depth'=>0,'value'=>'DEF'),
array('depth'=>1,'value'=>'DEF 1'),
array('depth'=>2,'value'=>'DEF_1'),
array('depth'=>1,'value'=>'DEF2'),
array('depth'=>0,'value'=>'GROUP'),
array('depth'=>1,'value'=>'GROUP1'),
array('depth'=>1,'value'=>'GROUP2'),
array('depth'=>2,'value'=>'GROUP2_1'),
array('depth'=>2,'value'=>'TELUGU'),
array('depth'=>3,'value'=>'RAM'),
array('depth'=>0,'value'=>'GROUP3'),
array('depth'=>1,'value'=>'GROUP3_1'),
array('depth'=>1,'value'=>'GROUP3_2'),
array('depth'=>1,'value'=>'GROUP3_3'),
array('depth'=>1,'value'=>'DEF2_1'),
array('depth'=>0,'value'=>'GROUP4'),
array('depth'=>0,'value'=>'T8')
);
$count = count($groups);
$old_depth = 0;
echo '<ul>'.chr(13).chr(10);
for($i = 0; $i<$count; $i++)
{
if($old_depth==$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if($old_depth!=$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
}
}
}
if($old_depth<$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if(($groups[$i]['depth'])+1==$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
if(!($groups[$i]['depth']==$groups[$i+1]['depth']))
{
echo '</ul></li>';
}
if($groups[$i+1]['depth']==0)
{
echo '</ul></li>';
}
}
}
else
{
echo '</ul>';
}
}
}
echo '</li></ul>';
?>
I want the output in this format.
<ul>
<li>DEF
<ul>
<li>DEF1
<ul>
<li>DEF_1</li>
</ul>
</li>
<li>DEF2</li>
</ul>
</li>
<li>GROUP
<ul>
<li>GROUP1</li>
<li>GROUP2
<ul>
<li>GROUP2_1</li>
<li>TELUGU
<ul>
<li>RAM</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>GROUP3
<ul>
<li>GROUP3_1</li>
<li>GROUP3_2</li>
<li>GROUP3_3</li>
<li>DEF2_1</li>
</ul>
</li>
<li>GROUP4</li>
<li>T8</li>
</ul>

Categories