The code below works great for paginating my custom post search results:
if( isset($_GET['pagination']) && $_GET['pagination'] != 'false'){
$arguments['posts_per_page'] = -1;
$pagination = 'true';
} else {
$arguments['posts_per_page'] = 9;
$pagination = 'false';
$arguments['order'] = 'ASC';
$arguments['meta_key'] = 'code_auto';
$arguments['orderby'] = 'meta_value';
}
However, if someone has filtered the results using this:
if( isset($_GET['sort_price'])){
$sort_price = $_GET['sort_price'];
if($sort_price == 'ASC'){
$arguments['order'] = 'ASC';
$arguments['meta_key'] = 'price-6';
$arguments['orderby'] = 'meta_value';
} else {
$sort_price = '';
$arguments['order'] = 'DESC';
$arguments['orderby'] = 'DATE';
}
}
The results paginate but don't include the sort_price query. Can anyone give any rewrites to include both arguments as i have tried many times but cant quite get it. Thank you!
Related
I have a problem code beneath this line does not work! How can I let this work? where ... orWhere orWhere does filter but cumulates the queries. where ... where does not provide any result. Can someone help me?
$artworks = Artwork::where('category_id', $category)
->where('style_id', $style)
->where('technic_id', $technic)
->where('orientation', $orientation)
->get();
Here is the full code:
if (request()->category_id) {
$category = request()->category_id;
} else {
$category = 0;
}
if (request()->style_id) {
$style = request()->style_id;
} else {
$style = 0;
}
if (request()->technic_id) {
$technic = request()->technic_id;
} else {
$technic = 0;
}
if (request()->orientation_id == 'vertical') {
$orientation = 'vertical';
} else if (request()->orientation_id == 'horizontal') {
$orientation = 'horizontal';
} else {
$orientation = 0;
}
$artists = Artist::get();
$artworks = Artwork::where('category_id', $category)
->where('style_id', $style)
->where('technic_id', $technic)
->where('orientation', $orientation)
->get();
return view('frontend.index', compact('artworks', 'artists'));
I think you want to use OR Condition and you are mistaking it with double where. Please look below to understand properly
If you want AND condition in your query then the double where are used but if you want OR condition then you have to use orWhere
Examples:
AND condition
Query::where(condition)->where(condition)->get();
OR Conditon
Query::where(condition)->orWhere(condition)->get();
If you expect all of your variables to be set
Your query variables category_id, style_id, orientation_id & technic_id are being defaulted to 0 if they are not true.
Your query is fine, but you may not have the data you think you do.
Run the following at the top of this function:
print_r($request->all());
exit;
If all of your variables are optional
very procedural, basic way to achieve this:
$artists = Artist::get();
$artworks = Artwork::where('id', '>', 0);
$category_id = request()->input('category_id');
if ($category_id != '') {
$artworks->where('category_id', request()->category_id);
}
$style_id = request()->input('style_id');
if ($style_id != '') {
$artworks->where('style_id', request()->style_id);
}
$technic_id = request()->input('technic_id');
if ($technic_id != '') {
$artworks->where('technic_id', request()->technic_id);
}
$orientation_id = request()->input('orientation_id');
if ($orientation_id != '') {
$artworks->where('orientation_id', request()->orientation_id);
}
$artworks->get();
return view('frontend.index', compact('artworks', 'artists'));
Wondering if anyone knows a better method that can teach me a smart way to handle this so i wont run into undefined warning when the xml comes back smaller
// Category
$main = $response->Items->Item->BrowseNodes->BrowseNode;
$cat1 = $main->Name;
$cat2 = $main->Ancestors->BrowseNode->Name;
$cat3 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
$cat4 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
//$cat5 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
//$cat6 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
//$cat7 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
//$cat8 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
//$cat9 = $main->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Ancestors->BrowseNode->Name;
switch ('Styles')
{
case $cat1;
$SubCategory = 'not found';
break;
case $cat2;
$SubCategory = $cat1;
break;
case $cat3;
$SubCategory = $cat2;
break;
case $cat4;
$SubCategory = $cat3;
break;
}
Use a loop in combination with a variable which holds the current node:
$curNode = $response->Items->Item->BrowseNodes->BrowseNode;
while ($curNode->Ancestors->BrowseNode) {
$curNode = $curNode->Ancestors->BrowseNode;
}
$subCategory = $curNode->Name;
You can also use a recursive function:
function getSubCategory($rootNode) {
if ($rootNode->Ancestors->BrowseNode) {
return getSubCategory($rootNode);
}
else {
return $rootNode->Name;
}
}
getSubCategory($response->Items->Item->BrowseNodes->BrowseNode);
Note that $var->Ancestors->BrowseNode checks for the existence of those keys in my pseudo code. You may need to replace it by ($var->Ancestors && $var->Ancestors->BrowseNode) in your real code.
I have a setup where it favourites and retweets a specific tweet. For some reason, the code works for the favourites however retweets do not. Can anyone see the issue?
$method = 'statuses/retweet/'.$url[3];
$amt = "26";
$sub = rand(1,3);
$amt1 = $amt-$sub;
if($_POST['favorite'] == "true" || $_POST['favorite'] == "1"){
for ($x1=1; $x1<=$amt1; $x1++)
{
$content = $connection[$x1]->post('favorites/create', array('id' => $url[3]));
}
}
if($_POST['retweet'] == "true" || $_POST['retweet'] == "1"){
for ($x2=1; $x2<=$amt; $x2++)
{
$content = twitteroauth_row('statuses/retweet/'.$url[3], $connection[$x2]->post($method), $connection[$x2]->http_code);
}
}
have you tried declaring
$amt = 26
instead of
$amt="26"
EDIT:
for ($x2=1; $x2<=amt; $x2++)
{
$content = twitteroauth_row('statuses/retweet/'.$url[3], $connection[$x2]->post($method), $connection[$x2]->http_code);
}
you have used amt instead of $amt in the loop condition
I have little programming experience so the jump from basic php to using classes, functions etc is a little daunting.
I'm building a little php script that works out what "theme" a user has selected via a form and from that selection, it loads some specific files. I've built a working script, but there's so much repetition in it, it's a joke.
// Add our theme names to variables
$theme1 = "theme1";
$theme2 = "theme2";
$theme3 = "theme3";
// Test to see what Theme the user chose.
// Theme 1
if ($themeChoice==$theme1)
{
// Load the theme
$homepage = file_get_contents('../themes/'.$theme1.'/index.html');
$mobile_js_main = file_get_contents('../themes/'.$theme1.'/js/main.js');
$mobile_js_jquery = file_get_contents('../themes/'.$theme1.'/js/jquery.js');
$mobile_css_easy = file_get_contents('../themes/'.$theme1.'/css/easy.css');
$mobile_images_bg = file_get_contents('../themes/'.$theme1.'/images/bg.png');
$mobile_images_footer = file_get_contents('../themes/'.$theme1.'/images/footer.png');
$mobile_images_nav = file_get_contents('../themes/'.$theme1.'/images/nav.png');
if ($AddPortfolioPage != '')
{
$portfolioPage = file_get_contents('../themes/'.$theme1.'/portfolio.html');
}
if ($AddContactPage != '')
{
$ContactPage = file_get_contents('../themes/'.$theme1.'/contact.html');
}
if ($AddBlankPage != '')
{
$blankPage = file_get_contents('../themes/'.$theme1.'/blank.html');
}
}
This is then repeated for each theme...which is obviously not an ideal method of doing this. Any help is much appreciated!
Put all of your themes into an array and loop to find the chosen one.
$themes = array( "theme1", "theme2", "theme3");
foreach( $themes as $theme)
{
if( $themeChoice == $theme)
{
$homepage = file_get_contents('../themes/'.$theme.'/index.html');
$mobile_js_main = file_get_contents('../themes/'.$theme.'/js/main.js');
$mobile_js_jquery = file_get_contents('../themes/'.$theme.'/js/jquery.js');
$mobile_css_easy = file_get_contents('../themes/'.$theme.'/css/easy.css');
$mobile_images_bg = file_get_contents('../themes/'.$theme.'/images/bg.png');
$mobile_images_footer = file_get_contents('../themes/'.$theme.'/images/footer.png');
$mobile_images_nav = file_get_contents('../themes/'.$theme.'/images/nav.png');
if ($AddPortfolioPage != '')
{
$portfolioPage = file_get_contents('../themes/'.$theme.'/portfolio.html');
}
if ($AddContactPage != '')
{
$ContactPage = file_get_contents('../themes/'.$theme.'/contact.html');
}
if ($AddBlankPage != '')
{
$blankPage = file_get_contents('../themes/'.$theme.'/blank.html');
}
break; // Break will exit the loop once the choice is found
}
}
Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}