Page 1 of 1
php display first.
Posted: Thu May 12, 2011 5:39 am
by aravona
Ok, so I've got a search which draws from a database based on user input, nice and simple it works well.
The issue I've got is with the display, I need it to display some options differently (due to image size) again not an issue, got this working. So search results A display in a grid view, and search results B display one at a time. However there is the chance that one search option inputted by the user shows BOTH options.
The problem I'm having is that I want to display all search results A first and all search results B second. At the moment, if it draws both styles then it displays haphazardly on the page.
Is there a php snippet of code I can add to my if statement to make search A display first then anything B.
Heres the code done simply as its very long:
Code: Select all
if ($option = X || $option = Y || $option = Z ... etc){
$class1 = A;
$class2 = AA;
}
else {
$class1 = B;
$class2 = BB;
}
echo "<div class='".$class1."'>";
echo "<div class='".$class2."'>";
echo some display ;
echo "</div>";
echo "</div>";
What I'm attempting to do is add something to force anything A to do the 'some display' code first, and then anything B to display afterwards. Its literally to make the layout look better. Though I may not be explaining very well!
Regards,
Aravona
Re: php display first.
Posted: Thu May 12, 2011 3:21 pm
by flying_circus
I think I understand what you are trying to do. Have a look at this psuedo-code and see if it will do what you need. You'll need to write your own version to make it work, but the idea is the same.
Code: Select all
<?php
# Execute Query - Fetch All Searched Images
$images = mysql_query("SELECT `images` FROM `database`;");
# Declare new array variables
$class_a_images = array();
$class_b_images = array();
# Parse Search Query Results
while($image = mysql_fetch_assoc($images)) {
# Determine if this is a class a image
if(image_is_class_a($images))
$class_a_images[] = $image;
# Determine if this is a class b image
if(image_is_class_b($images))
$class_b_images[] = $image;
}
# Output class a images
foreach($class_a_images as $image) {
// Add image to table layout
$table->add_image($image);
}
# Output class b images
foreach($class_b_images as $image) {
// Print out image element
print '<a href="images/' . $image . '" />';
}
?>
Re: php display first.
Posted: Fri May 13, 2011 4:24 am
by aravona
Thanks for the reply, I'll give it a shot (hack and slashing it around my current code!) and see how it pans out.
EDIT: I'm afraid what you said simply did not fit within the code I already had.
Code: Select all
function custom_search_request_wp($request) {
global $wpsc_query, $wpdb;
$wp_e_commerce_search_variables = $wpdb->escape(stripslashes($_GET['samleisure-search']));
$queryExtras = "list.name LIKE '%".$wp_e_commerce_search_variables."%'";
$sql="SELECT list.id,list.name,image.image
FROM ".$wpdb->prefix."wpsc_product_list AS list
LEFT JOIN ".$wpdb->prefix."wpsc_product_images AS image
ON list.image=image.id
WHERE ($queryExtras)
AND list.publish=1
AND list.active=1
ORDER BY CASE WHEN $queryExtras THEN 1 ELSE 0
END
";
//echo $sql;
$product_list = $wpdb->get_results($sql,ARRAY_A);
if (!$product_list) {
$output = "<p>There are no products found which contain the search term <i>".$wp_e_commerce_search_variables."</i>." . $sql;
}
else {
foreach((array)$product_list as $product) {
$sqlc = "SELECT `category_id` FROM `wp_wpsc_item_category_assoc` WHERE `product_id` = '".$product['id']."'";
$result2 = mysql_query($sqlc) ;
$category = mysql_fetch_row($result2);
if ($category[0] == '25' || $category[0] == '26' || $category[0] == '27' || $$category[0] == '28' || $category[0] == '29' || $category[0] == '30' || $category[0] == '31' || $category[0] == '32' || $category[0] == '33' || $category[0] == '34' || $category[0] == '35' || $category[0] == '36' || $category[0] == '37' || $category[0] == '38' || $category[0] == '39' || $category[0] == '40' || $category[0] == '41' || $category[0] == '42' || $category[0] == '43' || $category[0] == '44' || $category[0] == '45' || $category[0] == '46' || $category[0] == '47' || $category[0] == '48' || $category[0] == '49' || $category[0]== '50' || $category[0] == '51' || $category[0] == '52' || $category[0] == '53' || $category[0] == '54' || $category[0] == '55' || $category[0] == '107' || $category[0] == '108' || $category[0] == '109' || $category[0] == '110' || $category[0] == '111') {
$class_img = 'imagecol_group_wp_search_cue';
$class_txt = 'textcol_group_wp_search_cue';
}
else {
$class_img = 'imagecol_group_wp_search';
$class_txt = 'textcol_group_wp_search';
}
$output.="<div class='".$class_txt."'>";
$output .= "<div class='".$class_img."'>\n\r";
$output .= "<a href='".wpsc_product_url($product['id'])."'>";
if($product['image'] != '') {
$output .= "<img src='".WPSC_THUMBNAIL_URL.$product['image']."' title='".$product['name']."' alt='".$product['name']."' />\n\r";
$output .= "<p>\n\r";
$output .= stripslashes($product['name']);
$output .= "</a><br />";
$output .= "</p>\n\r";
}
$output .= "</div></div>\n\r";
}
$output .= "<br style='clear: left;'>\n\r";
return $output;
}
}
Thats the full code I'm working with, it
easily displays both styles, what I need is for it to display anything with the categories listed first. However, I can only pick up the category ID within this foreach, otherwise it just reads as 0.
Re: php display first.
Posted: Sun May 15, 2011 1:28 pm
by flying_circus
I would make some changes to your code. First, I would modify your original query to pull the category id at the same rather, rather than running n number of queries (1 for each returned product) in the foreach loop. This alone will net you quite a performance boost.
You need to divide the results and store them in a buffer for later use. The way your foreach loop is set up is that as it loops through the returned list of products, it goes first in first out. You cant group your images into 2 different display styles using that method. Using the foreach, make a decision based on the category id wether that product will be in group 1 (cue) or group 2 (not_cue). After the decision making is done, first output all of the group 1 (cue) products and then put out the group 2 (not_cue) products.
Here is an example code, you will have to debug it on your end, but this should give you an example to get you started.
Code: Select all
<?php
function custom_search_request_wp($request) {
global $wpsc_query, $wpdb;
$wp_e_commerce_search_variables = $wpdb->escape(stripslashes($_GET['samleisure-search']));
$queryExtras = "list.name LIKE '%".$wp_e_commerce_search_variables."%'";
$sql="SELECT list.id,list.name,image.image,category.category_id
FROM ".$wpdb->prefix."wpsc_product_list AS list
LEFT JOIN ".$wpdb->prefix."wpsc_product_images AS image
ON list.image=image.id
LEFT JOIN ".$wpdb->prefix."wpsc_item_category_assoc AS category
ON list.id=category.product_id
WHERE ($queryExtras)
AND list.publish=1
AND list.active=1
ORDER BY CASE WHEN $queryExtras THEN 1 ELSE 0
END
";
$product_list = $wpdb->get_results($sql,ARRAY_A);
if(!$product_list) {
$output = "<p>There are no products found which contain the search term <i>".$wp_e_commerce_search_variables."</i>." . $sql;
} else {
$cue = array();
$not_cue = array();
foreach((array)$product_list as $product) {
# Define group id's that are in 'cue'
$search_cue = array('25','26','27','28','29','30'); //etc...
# Divide Search Results into cue or not_cue
if(in_array($product[3], $search_cue))
$cue[] = array($product);
else
$not_cue[] = array($product);
}
# Output $cue products
foreach($cue as $product) {
$class_img = 'imagecol_group_wp_search_cue';
$class_txt = 'textcol_group_wp_search_cue';
$output="<div class='".$class_txt."'>";
$output .= "<div class='".$class_img."'>\n\r";
$output .= "<a href='".wpsc_product_url($product['id'])."'>";
if($product['image'] != '') {
$output .= "<img src='".WPSC_THUMBNAIL_URL.$product['image']."' title='".$product['name']."' alt='".$product['name']."' />\n\r";
$output .= "<p>\n\r";
$output .= stripslashes($product['name']);
$output .= "</a><br />";
$output .= "</p>\n\r";
}
$output .= "</div></div>\n\r";
}
# Output $not_cue products
foreach($not_cue as $product) {
$class_img = 'imagecol_group_wp_search';
$class_txt = 'textcol_group_wp_search';
$output="<div class='".$class_txt."'>";
$output .= "<div class='".$class_img."'>\n\r";
$output .= "<a href='".wpsc_product_url($product['id'])."'>";
if($product['image'] != '') {
$output .= "<img src='".WPSC_THUMBNAIL_URL.$product['image']."' title='".$product['name']."' alt='".$product['name']."' />\n\r";
$output .= "<p>\n\r";
$output .= stripslashes($product['name']);
$output .= "</a><br />";
$output .= "</p>\n\r";
}
$output .= "</div></div>\n\r";
}
$output .= "<br style='clear: left;'>\n\r";
return $output;
}
}
?>
Re: php display first.
Posted: Mon May 16, 2011 5:13 am
by aravona
Alas, didn't work at all, in fact it stopped the search working entirely. And when I tweaked my old code to use the additional inner join it messed the search bringing up multiple options of single products, I guess it'd need a distinct option in there. Wasn't any faster either. I think I'll have to stick with what I have already.
Re: php display first.
Posted: Mon May 16, 2011 12:16 pm
by Jade
It sounds like your tables aren't normalized or properly indexed.
Re: php display first.
Posted: Tue May 17, 2011 3:12 am
by aravona
The tables were made by a professional company, I have not touched them and am happy to leave them at the standard they are sold at.
Re: php display first.
Posted: Tue May 17, 2011 10:22 am
by flying_circus
aravona wrote:Alas, didn't work at all, in fact it stopped the search working entirely. And when I tweaked my old code to use the additional inner join it messed the search bringing up multiple options of single products, I guess it'd need a distinct option in there. Wasn't any faster either. I think I'll have to stick with what I have already.
I can't provide a complete code solution. I am not a wordpress developer, and I dont have the time to dedicate to debugging the project. The code I supplied was psuedo-code, meant to get the ball rolling and inspire you to see it through.
I'm certain that the solution I proposed is faster. With the code you provided, you run 1 query, and then run 1 additional query for each result. If your database returns 1,000,000 results, you will effectively run 1,000,001 queries for each time somebody requests the page. At this stage in the game, I assume you are still developing the application, which means that server load is very low. Since we are only dealing with fractions of a second (and probably few returned results), I wouldn't expect a human to be able to determine a performance gain yet. However, if you use a code profiler, you will likely see that inefficient code can quickly become a bottleneck when the database grows and the user base increases.
Good luck with your project! Hopefully I've contributed something worthwhile to think about.