PHP dynamic alphabetical list with headers - php

So what I am trying to produce is something like this:
Except, I do not want duplicates, when there is more than one sign, it becomes duplicated as well as re creating the "C" list view header. My SQL query is creating a column known as first_char which is holding the first character of one of the other columns.......below is my code:
<?php foreach ($data as $name=>$signs): ?>
<!-- If name is not empty, create the header -->
<?php if (isset($name) && !empty($signs)): ?>
<div data-role='collapsible-set' data-theme='d'>
<h2>Username: <strong id="headerName"><?=$name?></strong></h2>
<?php endif; ?>
<?php endforeach; ?>
<!-- for every sign, take the first char and create a list view -->
<?php foreach ($signs as $char):
$current_char = '';
if ($char['first_char'] != $current_char){
$current_char = $char['first_char'];
echo '<ul data-role="listview" data-dividertheme="d" style="margin-top: 0;"><li data-role="list-divider">'.$current_char.'</li></ul><br>';
}
foreach ($signs as $sign):
if($current_char == $sign['first_char'] and !empty($signs)):
?>
<div data-role='collapsible' data-collapsed='true' data-icon='arrow-l'>
<h3><?=$sign['sign_name']?></h3>
<div class='ui-grid-a'>
<div class="ui-block-a">
<div class="ui-bar ui-bar-d" style="height:25px;">
<strong>Last Seen:</strong><strong id="text"><?=$sign['last_connected']?></strong>
</div>
</div>
<div class="ui-block-a">
<div class="ui-bar ui-bar-d" style="height:25px;">
<strong>Resolution:</strong><strong id="text"><?=$sign['resolution_x'].'x'.$sign['resolution_y']?></strong>
</div>
</div>
<div class="ui-block-a">
<div class="ui-bar ui-bar-d" style="height:25px;">
<strong>Group Name:</strong><strong id="text"><?=$sign['group_name']?></strong>
</div>
</div>
<div class="ui-block-a">
<div class="ui-bar ui-bar-d" style="height:25px;">
<strong>Sign Number:</strong><strong id="text"><?=$sign['sign_id']?></strong>
</div>
</div>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
Thanks in advance

This fixes the previous error and gets rid of the duplication. $current_char needed to be outside the foreach loop, and removed the 2nd foreach.
<?php $current_char = '';?>
<?php foreach ($signs as $sign):?>
<?php
if ($sign['first_char'] != $current_char){
$current_char = $sign['first_char'];
echo '<ul data-role="listview" data-dividertheme="b" style="margin-top: 0;padding-top: 3px;"><li data-role="list-divider">'.$current_char.'</li></ul><br>';
}
?>
The query used above for SQL was this:
SELECT
user.username,
groups.group_id, groups.group_name,
sign.last_connected, sign.sign_id, sign.sign_name, sign.resolution_x, sign.resolution_y, LEFT(sign.sign_name, 1) AS first_char
FROM
user
LEFT JOIN(
groups, sign
)ON(
user.user_id = groups.userID AND
groups.group_id = sign.groupID
)
WHERE
username = ? AND
UPPER(sign.sign_name) BETWEEN "A" AND "Z"
OR sign.sign_name BETWEEN "0" AND "9" ORDER BY sign.sign_name

Related

How to show a specific <div> for a specific product?

I have setup a small webstore and I want to show this <div class ="data-plan"></div> only for one single product. My database table from where I want to match this id is called sb_files and it has these fields id table_id path type slide_link, so I am trying to get my code to go trough that table, search up the id ( Its 13040100 ) and if it matches then to show the div, else it shows an empty div. I am using the Yii2 framework, which is php based. So far I have tried this
<?php if($product->$this->id('13040100')): ?>
<ul>
<?php
$index = 1;
foreach($product->() as $prd):
if(strpos($prd->getPath(), '13040100') == true) {
?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php
$index++;
}
endforeach;
?>
</ul>
<?php else: ?>
<div class="wrap">
</div>
<?php endif; ?>
I'm not that familiar with Yii2 framework myself, but are you sure that $product->$this->id('13040100') is correct? It looks weird to me, shouldn't it be something like $product->id('13040100')?
The solution was way easier than I tought, just had to think simple
<?php if ($product->id == '13001100'): ?>
<div class="wrap">
<div class="data-plan" style="height:20px; width:65px; background-color:#00a651; float:right;color:white;margin:10px;text-align:center;">A+++ </div>"
<div class="data-plan" style="height:20px; width:65px; background-color:#ed1c1c; float:right;color:white;margin:10px;">Datu lapa </div>
</div>
<?php else: ?>
<div class="wrap">
</div>
<?php endif;

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

php code to display rows in two different display

I create two design to display news .
first DIV Display BIGGER images and Separate css class and display only two rows.
Second DIV Display Smaller images and Separate css class and display five rows..
I select rows in asc order from query.
my query is = select * from news order by priority asc.
My Only Need is TO Display two rows in First div and then display another rows in Second DIV.
IS This is possible using if else or any other condition...how to do this.........
but keep all div tag as it is bcoz of page design purpose......
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php foreach($top2 as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
</div>
<!-- end post -->
Yes, it's possible. Assuming your $top2 array has standard numerical/sequential keys, then:
$split = 2;
for($i = 0; $i < $split; $i++) {
... print $top2[0] and $top2[1]
}
for ($i = $split; $i < count($top2); $i++) {
... print the rest of the rows
}
Can use array_slice()
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 0, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
<!--<p class="details2">8 Comments / Read More </p>-->
</div>
</div>
<!-- end post -->
<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
<?php $arr = array_slice($top2, 2) ?>
<?php foreach($arr as $top){ ?>
<div class="content1"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php } ?>
</div>
Add a count that you increment in the foreach loop, then break when its over two:
<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
<?php
$rowCount = 0;
foreach($top2 as $top){ ?>
<div class="content"> <img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" />
<h2><?php echo htmlspecialchars_decode($top['headline']); ?></h2>
</div>
<?php
$rowCount++;
if($rowCount > 2){
break;
}
} ?>
<!--<p class="details2">8 Comments / Read More</p>-->
</div>
</div>
<!-- end post -->

Place DIV's in a containing DIV based on a numeric value

Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.

adding a more link based on the number of entries

I have this code that prints out up to 4 service providers. once clicked, the box expands and shows the rest if there are more. What i am trying to do is if there are more than 4 entries, to print a More link so users know to click for more. I also need the More button to disappear when click and reappear when reclicked.
Can anyone help me. This is driving me crazy
Thank you for your help ahead of time.
<?php
/**
* #file
*/
?>
<head>
<script>
var remove=function(){
$('#ID_OF_BUTTON').hide(500);
}
</script>
</head>
<div class="cloud-computing-item">
<div class="container">
<div class="item-header">
<h3> <?php print $company['name'] ?> </h3>
</div>
<div class="item-subheader">
<div class="label">Services Offered:</div>
<div class="data service-offerings">
<?php
foreach($company['services_display'] as $service => $element){
print $element;
}
?>
</div>
</div>
<div class="item-body">
<div class="overview">
<div class="label">Cloud Providers:</div>
<div class="data">
<?php
//limit shown entries upto 4
foreach(array_slice($company['service_providers'], 0, 4) as $provider): ?>
<div>
<?php print $provider; ?>
</div>
<?php endforeach; ?>
<div id="show"style="color:#000099;font-weight:bold; margin-bottom:-13px;"></div>
<!--<div id="show"style="color:#000099;font-weight:bold; margin-bottom:-13px;"><a onclick="remove(); return true;">More</a></div>
<div id="hide"style="color:#000099;font-weight:bold; margin-bottom:-12px; display: none;"><a onclick="add(); return true;">Hide</a></div> -->
</div>
</div>
<div class="details">
<?php
// if entries are greater then 4, show the rest
foreach(array_slice($company['service_providers'], 4) as $provider): ?>
<div>
<?php
print $provider;
?>
</div>
<?php endforeach; ?>
<?php print theme('cloud_computing_item_details', array('company' => $company)); ?>
</div>
</div>
<div style="clear: both; height: 5px;"> </div>
</div>
</div>
So, try this modification:
<div class="label">Cloud Providers:</div>
<div class="data">
<?php $i = 0; ?>
<?php foreach($company['service_providers'] as $provider): ?>
<div<?php echo ($i > 3) ? ' class="hide"' : ''; ?>><?php print $provider; ?></div>
<?php $i++; ?>
<?php endforeach; ?>
<?php if(count($company['service_providers']) > 4):?>
<div class="show-more" style="color:#000099;font-weight:bold; margin-bottom:-13px;">Show More</div>
<?php endif; ?>
</div>
Now let's assume You are using jQuery add this:
<script type="text/javascript">
$(document).ready(function(
$('.hide').hide(); // this will hide all the elements with the class 'hide'
$('.show-more').live('click', function(){
var parent = $(this).parent();
parent.find('.hide').show().removeClass('hide').addClass('show');
$(this).text('Show Less').removeClass('show-more').addClass('show-less');
});
$('.show-less').live('click', function(){
var parent = $(this).parent();
parent.find('.show').hide().removeClass('show').addClass('hide');
$(this).text('Show Less').removeClass('show-less').addClass('show-more');
});
){});
</script>
Use function on instead of live if You are using jQuery higher then 1.7.
Fiddle: http://jsfiddle.net/eznnC/ (though the hiding is not working but I believe it will in normal environment).
the first part to make only the 4 or more show a More link is below. the link doesn't disappear at the moment nor does it reappear.
<?php
//limit shown entries upto 4
foreach(array_slice($company['service_providers'], 0, 4) as $provider): ?>
<div>
<?php print $provider; ?>
</div>
<?php endforeach; ?>
<?php
// shows a More link for companies with more than for providers
foreach(array_slice($company['service_providers'], 4, 1) as $provider): ?>
<div>
<div id="more" style="color:#000099;font-weight:bold; margin-bottom:-13px;">
<?php print ('<a onclick="">More</a>'); ?>
</div>
</div>
<?php endforeach; ?>

Categories