I'm really struggling with something and wondered if anyone could spare a few moments to have a look at this code block.
The original line looked like this:
$home_collectionsx=get_home_page_promoted_collections();
This brought back all the promoted to homepage items and displayed them on the homepage. I however simply want to pull 1 item in using the same code and an array function, the id is 5 for this purpose so I thought adding =array(5) or (array (5)) would work - but it doesn't.
I'm hoping it's something simple, or something that I have missed or not written correctly.
<?php
if(!hook("EditorsPick")):
/* ------------ Collections promoted to the home page ------------------- */
$home_collectionsx=get_home_page_promoted_collections (array(5));
foreach ($home_collectionsx as $home_collectionx)
{
?>
<div class="EditorsPick">
<div class="HomePanel"><div class="HomePanelINtopEditors">
<div class="HomePanelINtopHeader">Editors Pick</div>
<div class="HomePanelINtopText">This is the editors pick of Asset Space...</div>
<div class="EditorsPicImage"><div style="padding-top:<?php echo floor((155-$home_collectionx["thumb_height"])/2) ?>px; margin-top: -24px; margin-bottom: -15px;">
<a href="<?php echo $baseurl_short?>pages/search.php?search=!collection<?php echo $home_collectionx["ref"] ?>" onClick="return CentralSpaceLoad(this,true);"><img class="ImageBorder" src="<?php echo get_resource_path($home_collectionx["home_page_image"],false,"thm",false) ?>" width="<?php echo $home_collectionx["thumb_width"] ?>" height="<?php echo $home_collectionx["thumb_height"] ?>" /></div>
</div></div>
</div>
</div>
</div>
<?php
}
endif; # end hook homefeaturedcol
?>
This is the function to the DB itself that the above code is connecting to…
function get_home_page_promoted_collections()
{
return sql_query("select collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width from collection left outer join resource on collection.home_page_image=resource.ref where collection.public=1 and collection.home_page_publish=1 order by collection.ref desc");
}
Any help would be hugely appreciated :-)
Many many thanks
Rich
That function doesn't take a parameter: get_home_page_promoted_collections()
You want something like:
$home_collectionsx=get_home_page_promoted_collections(5);
And:
function get_home_page_promoted_collections($id=null)
{
$filterClause = '';
if(!is_null($id))
{
//to only return this id
$filterClause = ' AND collection.ref = '.intval($id);
//to get all but that id
$filterClause = ' AND collection.ref != '.intval($id);
}
return sql_query("SELECT collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width FROM collection LEFT OUTER JOIN resource on collection.home_page_image=resource.ref WHERE collection.public=1 AND collection.home_page_publish=1".$filterClause." ORDER BY collection.ref DESC");
}
Related
I've the following query
As you see jigsaw repeats twice because the movie has two categories
but i would like to echo both categories but not twice the movie..
<?php
while ($mInfo = $testquery->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="col-xs-6 col-sm-4 col-md-3">
<a href="movie.php?title=<?php echo $mInfo['titleEN']; ?>&m=<?php echo $mInfo['imdbID']; ?>" title="<?php echo $mInfo['titleEN']; ?>" alt="<?php echo $mInfo['titleEN']; ?>" target="_self">
<div class="movie-info">
<img src="assets/images/poster/<?php echo $mInfo['poster']; ?>.jpg" alt="<?php echo $mInfo['titleEN']; ?>">
</div>
<div class="movieinformation">
<div class="movie-title"><?php echo $mInfo['titleEN']; ?></div>
<div class="movie-categories">I WOULD LIKE TO ECHO THE CATEGORIES HERE</div>
</div>
</a>
</div>
<?php
}
?>
So far i just could do it, could anyone help me with that?
Here's a concept for such tasks. I kept it with general names and left out html on purpose.
$oldMainId = null; // Initialize to null to start with. This will be needed later.
while ($item = $result->fetch()) { // get your items (movies in your case)
if($oldMain != $item['mainId']) {
// only show title if you haven't yet
echo $item['mainTitle'];
}
// always show the category though
echo $item['sub'];
// re-set the 'old' variable.
$oldMainId = $item['mainId'];
}
I would use
OPTION 1
$res = $stmt->fetchAll(PDO::FETCH_GROUP);
Then it will group on the first column, assuming that is movie ID, or something unique to the movie you would get multiple rows in a nested array for that movie.
Then when you iterate thorough them you can loop over the nested array for the genre stuff
OPTION 2
Another option is to use GROUP_CONCAT and group by the movie id,
SELECT GROUP_CONCAT(genre) AS genre, ... WHERE ... GROUP BY movieID
But be aware that GROUP_CONCAT does have a setting for max length and it will silently truncate your data if it exceeds it.
OPTION 3
You can build the structure yourself (same as fetch group does)
$data = [];
while ($mInfo = $testquery->fetch(PDO::FETCH_ASSOC)) {
$key = $mInfo['movieid']
if(!isset($data[$key])) $data[$key] = [];
$data[$key][] = $mInfo;
}
Then go through that and do your html by using a second foreach($data as ...) It has to be done after organizing the data as the order of the result is unknown.
I am currently facing a nightmare. My DB has only 400 Records almost and the problem is that my PHP code which is generating Image Sliders so the code is taking like 60 Seconds almost to get executed for only 400 Records which is way too much time, No one can bear to wait for that much time.
I am really worried about it as I am not sure what could be done which could make it faster but so far to my knowledge is that by using foreach loop instead of for loop can make things faster but I want to know if you people can please provide me with solutions.
It's a Partial Template inside a Symfony Site which renders the Image Slider and fetching slider each image from the DB. But I am not worried about the SQL Queries as that only takes about 3 to 4 seconds at most to execute all queries to get these slider images but this code which generates the slider takes like a hell of a lot more time almost 60+ Seconds in most cases which is too much time for anyone to wait around.
If you people can provide me with optimized code for best performance, I would be grateful for that.
Here the stats as :
Here is my code as (_searchResults.php) :
<?php if (count($locations) > 0): ?>
<?php foreach ($locations as $location): ?>
<?php if ($location->PhotoFiles->count()): ?>
<li class="item" data-location-id="<?php echo $location->id ?>">
<div class="searchItemSlider">
<?php $limit = ($location->PhotoFiles->count() >= 10 ? 10 : $location->PhotoFiles->count()); ?>
<?php for ($i = 1; $i < ($limit + 1); $i++): ?>
<?php $photo = $location->findPhotoIndex($i); ?>
<?php //if (1): ?>
<?php if ($photo && $thumb = $photo->getThumbnailByType(ThumbnailTable::THUMB_520_392)): ?>
<div class="searchItem">
<figure>
<?php if (file_exists($thumb->getFullFilesystemPath())): ?>
<?php //if (1 == 2): ?>
<div class="thumb-img">
<img class="lazyOwl" src="<?php echo $thumb->getFullWebPath() ?>" />
</div>
<?php else: ?>
<div class="thumb-img">
<img class="lazyOwl" src="/images/newfrontend/categories/no_venue.jpg" />
</div>
<?php endif; ?>
<figcaption>
<?php echo $location->venue_name; ?>
<br />
</figcaption>
<?php $districtName = (strlen($location->venue_area)) ? $location->venue_area : $location->getGeoLocationDistrictName();
if (strlen($districtName)): ?>
<div class="district_name"><?php echo $districtName ?></div>
<?php endif; ?>
</figure>
<p><?php echo substr(strip_tags(html_entity_decode($location->description)), 0, 100) ?></p>
<span class="eye"><img src="/images/newfrontend/icons/eye-icon.png"/></span>
<div class="links">
<?php if (!$sf_user->locationInWishlist($location->id)): ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endfor; ?>
</div>
</li>
<?php else: ?>
<li class="item" data-location-id="<?php echo $location->id ?>">
<div class="searchItemSlider">
<div class="searchItem">
<figure>
<div class="thumb-img">
<img class="lazyOwl" src="/images/newfrontend/categories/no_venue.jpg" />
</div>
<figcaption>
<?php echo $location->venue_name; ?>
<br />
</figcaption>
<?php $districtName = (strlen($location->venue_area)) ? $location->venue_area : $location->getGeoLocationDistrictName();
if (strlen($districtName)):
?>
<div class="district_name"><?php echo $districtName ?></div>
<?php endif; ?>
</figure>
<p><?php echo substr(strip_tags(html_entity_decode($location->description)), 0, 100) ?></p>
<span class="eye"><img src="/images/newfrontend/icons/eye-icon.png"/></span>
<div class="links">
<?php if (!$sf_user->locationInWishlist($location->id)): ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
</div>
</li>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
Update :
Location : /lib/model/doctrine/PhotoFile.class.php
/**
* Returns the first thumbnail of a certain type
* or false if no thumbnail matches
*
* #param const $type The type of thumbnail, from the ThumbnailTable::THUMB_* constants
* #return bool|Thumbnail
*/
public function getThumbnailByType($type) {
foreach ($this->Thumbnails as $t) {
if ($t->thumb_type == $type) {
return $t;
}
}
return false;
}
Location : /lib/model/doctrine/Location.class.php
/**
* Geocoder: get district name (administrative county)
*
* #return string
*/
public function getDistrictName()
{
return ($this->hasGeocode() ? $this->getGeocode()->district_name : null);
}
public function getGeoLocationDistrictName()
{
$districtName = '';
if($this->getGeocoded()->count() > 0)
{
if($this->getGeocoded()->getFirst()->getGeolocationPostcode()->count() > 0)
{
if($this->getGeocoded()->getFirst()->getGeolocationPostcode()->getFirst()->getGeolocationDistrict()->count() > 0)
{
$districtName = $this->getGeocoded()->getFirst()->getGeolocationPostcode()->getFirst()->getGeolocationDistrict()->getFirst()->getName();
}
}
}
return $districtName;
}
/**
* Gets the geocode data for this location.
* We store it locally so we don't need to keep querying for it.
*
* #return boolean|PcawCodes $pcawCode
*/
public function getGeocode()
{
if ($this->Geocoded->count())
{
return $this->Geocoded->getFirst();
}
if (!strlen($this->address_postcode))
{
// No postcode to geocode
return false;
}
$geocode = new JointGeocoder();
try
{
/* #var $result PcawCodes */
$result = $geocode->geocode("", $this->address_postcode);
// Add the postcode also to the GeolocationPostcode table
if($result) Doctrine::getTable('GeolocationPostcode')->addPostcodeIfMissing($result);
}
catch (Exception $e)
{
// Some problem occurred geocoding, return false
return false;
}
return $result;
}
I note that the benchmarking tool you're using seems to indicate the page is making 4540 queries. If so that is probably the cause.
Based on your code I suspect this is the n+1 problem - essentially making another query inside a loop by fetching a new model individually. Most ORMs have a mechanism to solve that via eager loading, whereby they fetch all of those rows beforehand. Using eager loading also makes it easier to cache the response.
I'm not familiar with Doctrine, but according to https://tideways.io/profiler/blog/5-doctrine-orm-performance-traps-you-should-avoid it supports eager loading.
I'm not going to specifically tell you how you can fix this problem, but instead make suggestions for improving performance.
3 - 4 seconds for your queries to complete is a LONG time. Optimise your database schema, analyse queries, use appropriate indexes.
Only query the data that you need. If you don't need 400 results, limit your query appropriately.
How often does this data really change? Does it always need to be up-to-date? Parse the data into the structure you need in your controller or a service class and then cache that to a file or similar.
Read the cached data, rather than hitting the database every time.
Do not generate thumbnails on the fly or at time of need. Generate them when uploading, or generate them once and cache them for future use.
Build your view and cache your view, so that you're again not hitting the database every time.
Serve your content with appropriate cache headers so the page and/or assets are cached by the user's browser.
I have created a homepage editor tool in a script I purchased. The function of this homepage editor is to allow me to create different sections and display them one on top of the other in the order they are created. Which in hopes will give me an effect of several blocks that stretch width of the screen.
All seems to work well except one piece. I input my html and php code into the field in the admin panel and it saves to the db as I wrote it. However, when I go to echo each section back to the homepage it just displays my php code as plain text and doesn't interpret it as php and do its function.
Here is code from the homepage.php that prints the results.
<?php
session_start();
require_once("inc/config.inc.php");
if (isset($_GET['ref']) && is_numeric($_GET['ref']))
{
$ref_id = (int)$_GET['ref'];
setReferal($ref_id);
header("Location: index.php");
exit();
}
/////////////// Page config ///////////////
function get_all_section($section_id='')
{
$sql="SELECT * FROM `cashbackengine_homepage` WHERE 1";
if($section_id!="")
{
$sql.=" AND section_id='".$section_id."'";
}
$sql.=" AND section_status=1";
$sql.=" ORDER BY section_order ASC";
//echo $sql;
$res=mysql_query($sql);
while($row=mysql_fetch_array($res))
{
$section_array[]=array(
'section_id' =>$row['section_id'],
'section_name' =>$row['section_name'],
'section_desc' =>$row['section_desc'],
'section_order' =>$row['section_order'],
'section_status' =>$row['section_status'],
'last_updated' =>$row['last_updated'],
);
}
return $section_array;
}
$get_all_section=get_all_section('');
/*$get_all_section2=get_all_section('2');
$get_all_section3=get_all_section('3');
$get_all_section4=get_all_section('4');
$get_all_section5=get_all_section('5');*/
for($i=0; $i<count($get_all_section);$i++)
{
//echo htmlspecialchars_decode($get_all_section[$i]['section_desc']);
//echo htmlspecialchars_decode(stripslashes(str_replace(" ","",(str_replace("<br />","\n",$get_all_section[$i]['section_desc'])))));
echo $get_all_section[$i]['section_desc'];
}
?>
I am certain the problem has to do with the echo at the end. But I am unsure how to use htmlspecialchars to make it work with php if it even will. Or if I have to put something weird in my saved section.
Here is one of my sections. Any help is greatly appreciated. Thank you.
<div style="height:260px; width:100%; background-color:#000; margin:0px; color:white;">
<div id="header">
<div id="logo"><img src="<?php echo SITE_URL; ?>images/logo.png" alt="<?php echo SITE_TITLE; ?>" title="<?php echo SITE_TITLE; ?>" border="0" /></div>
<div class="start_saving">
<div id="links">
<?php if (MULTILINGUAL == 1 && count($languages) > 0) { ?>
<div id="languages">
<?php foreach ($languages AS $language_code => $language) { ?>
<img src="<?php echo SITE_URL; ?>images/flags/<?php echo $language_code; ?>.png" alt="<?php echo $language; ?>" border="0" />
<?php } ?>
</div>
<?php } ?>
<div id="welcome">
<?php if (isLoggedIn()) { ?>
<?php echo CBE_WELCOME; ?>, <span class="member"><?php echo $_SESSION['FirstName']; ?></span><!-- | <?php echo CBE_ACCOUNT ?>--> | <?php echo CBE_BALANCE; ?>: <span class="mbalance"><?php echo GetUserBalance($_SESSION['userid']); ?></span> | <?php echo CBE_REFERRALS; ?>: <span class="referrals"><?php echo GetReferralsTotal($_SESSION['userid']); ?></span>
<?php }else{ ?>
<a class="signup" href="<?php echo SITE_URL; ?>signup.php"><?php echo CBE_SIGNUP; ?></a> <a class="login" href="<?php echo SITE_URL; ?>login.php"><?php echo CBE_LOGIN; ?></a>
<?php } ?>
</div>
</div></div>
</div>
It looks like you're getting these section contents pieces out of your database, and not from a file stored on your web server. Is that correct?
Assuming that's true, then my next question would be, who populates this data? Is this taken in any way from user input? The reason why I ask is because of my next suggestion, which may or may not be received well.
The reason why your PHP code isn't executing, is because it's being retrieved from the database and output as a string, not as code. So how do you execute code that's stored in a string, you ask? Well, the answer to that question is to use eval() on the string. But this is where you have to be really careful!!!!!!! If any part of that string could have possibly come from an untrusted source, then malicious PHP code could be executed, which could potentially give evildoers a way into your server, where they can find all the information in your database, server, etc. Make sure you know where your code is coming from before executing it!
You make a good point that it's HTML mixed with PHP. So I see two possible solutions...
This post suggests that you could do eval(' ?>'. $section .' <?php'); This makes sense, you're breaking out of PHP before you eval your string, and so requiring the included string to open its own PHP tags to write PHP code.
Another way I can think of would be to throw the contents into a temporary file, and then include() that file:
// get contents, store in $contents
$filename = tempnam(sys_get_temp_dir(), 'section');
file_put_contents($filename, $section);
include($filename);
unlink($filename);
I am trying to display a 'meet the team' page in a 2x5 table, working with code written by the previous web designer in Wordpress.
This is the page I am looking at - http://www.stirling-house.com/about-us/meet-the-team As I say, I want to display the pictures in a table, so it looks neater.
The code I have is -
<div id="maintext">
<h1>MEET THE TEAM</h1>
<h3>THE STIRLING HOUSE ADMINISTRATION TEAM</h3>
<?php $members =get_posts('numberposts=99&cat=4&order=ASC'); //print_r($members);
foreach ($members as $post) {
setup_postdata($post);?>
<div class="member-box">
<img src="<?php echo get('member_photo');?>" alt="" style="margin:5px 7px 0 0;" />
<h3><?php echo $post->post_title;?></h3>
<h4><?php echo get('member_designation');?></h4>
Can anyone help?!
One way of doing it is by adding a counter before the foreach (for example, $n), initialize it to zero and increment it every time the loop finishes. Then, make all the odd divs float to the left and the even ones float to the right.
This is how the code would look like (more or less):
<?
$n = 0; // Counter
foreach($members as $post){
if($n % 2) $style="float:left";
else $style="float:right";
<img src="<?php echo get('member_photo');?>" alt="" style="margin:5px 7px 0 0;" />
<h3><?php echo $post->post_title;?></h3>
<h4><?php echo get('member_designation');?></h4>
If that does not work, you can try using a table layout and placing a <tr> tag after $n rows (resetting the counter every time you add a <tr>).
It is the opinion of most web designers (post-2005 or so) that <table> elements should not be used for layout/design. If you add the following CSS rule to your sites, stylesheet:
.member-box { display: inline-block; }
You will get something that looks like this: http://imgur.com/DzEkLAU
Sorry to answer indirectly, but I think this is the answer you're really looking for... ;)
I'm creating a web site directory for my mobile site (FOUND HERE)
I have figured out how to display listings from my mysql table to my home page from my tables promo_cat list.
The thing im having trouble with is this:
once clicking on one of the catagories it leads me to my list.php page.
How do I get this page to display results related to the category clicked and not others?
For example:when clicking on "FREE" brings up this page: http://www.xclo.mobi/xclo2/list.php?id=FREE. Which displays all results. it should only display results that have a promo_cat field as "FREE" and should not display any other results as it does currently.
My list.php code:
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>xclo mobi</title>
<link rel="stylesheet" href="other.css" />
</head>
<body>
<?php include_once('header.html'); ?>
<div class="container">
Category = ???
<ol>
<?php foreach ($articles as $article) { ?>
<div class="border">
<a href="single.php?id=<?php echo $article['promo_title']; ?>" style="text-decoration: none">
<img src="<?php echo $article['promo_image']; ?>" border="0" class="img" align="left"><br />
<img alt="" title="" src="GO.png" height="50" width="50" align="right" />
<font class="title"><em><center><?php echo $article['promo_title']; ?></center></em></font>
<br /><br />
<font class="content"><em><center><?php echo $article['promo_content']; ?></center></em></font>
</div><br/><br />
</a>
<?php } ?>
</ol>
</div>
</body>
</html>
/include/article.php
<?php
class article {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_title) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi WHERE promo_title = ?");
$query->bindValue(1, $promo_title);
$query->execute();
return $query->fetch();
}
}
?>
You need to make changes to the code for list.php based on the input it gets through GET parameter. something like:
if ($_GET['id'] == 'FREE'){
// do something like display FREE items
}
elseif($_GET['id'] == 'GIFT') {
// display GIFT items
}
else {
// perform some default action
}
This is to make it even more database driven (helpful when there are many categories):
$sql = "select * from categories where id = '".$_GET['id']."'";
if (mysql_results($sql)){
// do something
}
else {
// show error
}
Note that this is for demo only and in your code you should use PDO/MySQLI and prepared statements and not mysql_results function.
In light of more information provided by OP:
Change this
$articles = $article->fetch_all();
to
$articles = $article->fetch_data($_GET['id']);
in list.php and see if you get correct results.
Based on the code you provided, try this:
<?php foreach ($articles as $article) {
if ($article['promo_cat'] === 'FREE') { ?>
// Keep the rest of the code
//instead of <?php } ?> - put...
<?php } } ?>
Keep in mind, this is messy. But the foreach statement (I imagine) is being used to print out all posts. So, before printing out a post, you just check to see if the promo_title is FREE, GIFT, etc. If it's not, then it doesn't print that item.
You can make this more dynamic by passing in a $_GET variable (which you apparently are doing, but the code is never using this variable) with the current promo title and altering the conditional line to say
if ($article['promo_cat'] === $_GET['id'])
Hope that helps!