Displaying Search Fields - php

This may not be enough information for anyone to answer this, but I might just be missing something y'all would know by looking at the code.
In my site's admin panel, I can assign custom fields to categories (No. of beds, baths, whatever I create), but in order for the fields to show up on the Advanced Search page, you have to assign the field(s) to all of my sites categories.
I am trying to get the field(s) to show up all the time, even if they are only assigned to certain categories, not all of them.
Here is the code that renders the fields on the Advanced Search page, but again, only renders them if the field is assigned to all categories:
<?php
$get_catID = get_CATID($_GET['ad_cat_cat']);
if(empty($get_catID)) $get_catID = 0;
$get_catID = array($get_catID);
$arr = get_category_fields_without_vals($get_catID, 'no');
for($i=0;$i<count($arr);$i++)
{
echo '<tr>';
echo '<td>'.$arr[$i]['field_name'].$arr[$i]['id'].':</td>';
echo '<td>'.$arr[$i]['value'].'</td>';
echo '</tr>';
}
?>

Related

PHP - Retrieving data from repeater fields in WordPress

I have a custom field in WordPress called listing-two. listing-two is a simple repeater field which allows you to add another item to a list.
Since a list may have x amount of listings, what is the best way to retrieve the data from that field? At the moment I have
'listingTwo'=> get_field('listing-two')
Which will not work since there are multiple fields?
I've seen the AFC documentation to get an idea of how to get data, but none of them seem to work?
Edit:
I'm thinking the best way to go about this is via a for loop? I've tried the following, still not pulling anything through:
$textareaTwo = get_sub_field("listing-two");
if ($textareaTwo && count($textareaTwo)>0){
foreach ($textareaTwo as $textareaTwos){
$res = get_post($textareaTwos);
echo'Test'.$res;
}
}
Repeater field structure:
Field name: listing-two -> Has a sub field, with the field name list_item
Try this,
if(get_field('listing_two')){
$lists = get_field('listing_two');
foreach($lists as $list){
echo $list;
}
}
below is the code by which you can get all fields within listing_two
<?php if( have_rows('listing_two') ):
while( have_rows('listing_two') ): the_row();
// vars
$sub_field1 = get_sub_field('sub_field1');
$sub_field2 = get_sub_field('sub_field2');
$sub_field3 = get_sub_field('sub_field3');
echo $sub_field1;
echo $sub_field2;
echo $sub_field3;
endwhile;
endif; ?>

Wordpress - how to define cities dropdown values

I am working on an existing wordpress website.
Users has field "user-country" (actually, I do not know how this field is created in wordpress, but it works).
In the registration form, user can choose one specific country.
However, now this country list is note defined "anywhere". It is created explicitly in the code:
<option value="Afghanistan" <?php if($current_user->user_country == 'Afghanistan') echo 'selected';?>>Afghanistan</option>
<option value="Albania" <?php if($current_user->user_country == 'Albania') echo 'selected';?>>Albania</option>
<option value="Algeria" <?php if($current_user->user_country == 'Algeria') echo 'selected';?>>Algeria</option>
<option value="American Samoa" <?php if($current_user->user_country == 'American Samoa') echo 'selected';?>>American Samoa</opt
etc.
The client wants to changed this list (from country to city). So i need to add other values. I do not want to write all values in the code. I would like to create some list with these values in wp-admin.
What is the best way to create a predefined values list? And these are not custom fields for posts.
EDIT:
I want to store values in DB, so admin can modidfy these values from wp-admin.
Actually, it is not so important whether it is DB or other option like XML.
I just want this list to appear as dropdown when user is registering and also to wp-admin to modify values of this list.
Also, a question come to my mind - is it a normal practice to store user custom fields like country or city in DB? Or maybe it is ok to define them in code explicitly?
Well, if you want the administrator to be able to modify the list, then DB is likely the best option here.
I would do something like this (in WordPress):
// put a default (initial) list in the database, if there isn't one there yet
if(!get_option('my_country_list')){
// store it as a |-delimited string, because WP serializes arrays,
// and this would be too much here
$data = 'Albania|Algeria|Disneyland|etc';
update_option('my_country_list', $data);
}
Now, later where you need that list, simply get it from the db:
$countries = get_option('my_country_list');
// turn it into an array
$countries = implode('|', $countries);
// generate the select field
$html = '';
foreach($countries as $country){
$checked = '';
if($current_user->user_country == $country)
$checked = 'selected="selected"';
$html .= sprintf('<option value="%1$s" %2$s> %1$s </option>', $country, $checked);
}
printf('<select> %s </select>', $html);
I guess you'll also have some kind of administration form for the options, where the administrator can modify entries from this list. This could be a textarea. When it gets submitted you update_option() again (replace new lines with |)

Not understanding how to add cart into PHP

I am stuck on this code. I am making a web page and on the side there is a place for a cart. And with the you should be able to click on an item and add it to cart. Well I am having trouble getting it to add it to cart. Can someone help me understand what I should be doing. I have been working on it for a few days and no matter what I am doing nothing is working. If i get the code to show you have 0 in your cart it wont add anything if i try to put it in the cart.
<h1>Cart Contents?</h1>
<div class="p2">
<?php
// Get all the categories and
// link them to category.php.
// Define and execute the query:
$q = 'SELECT category_id, category FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);
// Fetch the results:
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Print as a list item.
echo "<li>$fcat</li>\n";
if($_SERVER['PHP_SELF']!="CART FILE"){
echo "<h1>Cart Contents</h1>";
echo "<div class=\"p2\">";
$itemCount=X;
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
echo "You have ".$itemCount." total items in your cart.";
echo "</div>\n";
} // End of while loop.
?>
<h1>Specials?</h1>
<div class="p2">
<p>Maybe place specials or new items or related items here.</p>
</div>
</div>
<div class="content">
Ok here is a link to what the cart should do if you look over to the side it should do what that one is doing.
http://www.programmerskit.com/advPHP/ch5/
Shouldn't
$itemCount=X;
foreach($_SESSION['cart'] as X=>X){
for($i=0;$i<count(X);$i++){
$itemCount+=X;
}
}
just be:
$itemCount = count($_SESSION['cart']);
I can't otherwise figure out what that code is supposed to be doing.
Also, that code that outputs the cart appears to be in a while loop outputting each item category, so you will be displaying the cart multiple times, which I can only assume is not desired functionality.
Also, another poster made a point about the invalid use of X as a constant, which is also a good point.
You've got a bare X used all over the place. While saying
$somevar = X;
would be legitimate if you'd already done define('X', 'somevalue') previously, this next one
foreach($_SESSION['cart'] as X=>X){
is completely invalid. You cannot assign new values to a defined constant, let alone try to assign TWO different values at the same time
foreach($_SESSION['cart'] as $key => $value)
is how that particular bit of code should be.

Wordpress - Display text if checkbox output matches the page title

I want to show the projects that has had it's checkbox ticked as Branding, if it's on the Branding page (i.e the page title is Branding).
To explain the code a bit:
This line show's all the checkboxes that have been ticked for each project so it will output "Branding", "Web", "Print" if they have been ticked.
implode(', ',get_field('categories')
This next line is just checking the page title is "Branding":
implode(', ',get_field('categories')
I'm trying to put these both in an if statement where it would just output the checked boxes and if they match the title then output them.
<?php if(implode(', ',get_field('categories')) && $grid_title == "Branding"); {
echo "testing";
}
?>
The code above shows what I want to do but it doesn't quite work.
IMPORTANT: I'm using this plugin to create the custom checkboxes so please bear that in mind.
=============================
UPDATE:
Thanks very much to Adam Kiss for solving what I asked, small update to question:
How could I code this neatly - using your answer, Branding was just one example of the check boxes, there's also several other one's like Web, Print, Social so how could I match those to the page title as well?
So it will be along the lines of if checked field equals the page title "branding" do OR checked field equals page title "web" OR checked field equals page title "print".
the function you're looking for is in_array:
<?php
if(
in_array("Branding", get_field('categories'))
&& $grid_title == "Branding"
){
echo "testing";
}
Note: this assumes that result of that implode is array with strings like "Branding", "Web", etc.
Edit: Since we're using implode(), I assume get_field returns type array, so we put the implode away (I got confused for a while)
Edit: Sorry, was away :]
you could use array_intersect
Usage:
$categories = get_field('categories');
$cats_iwant = array("Branding", "Print", "Design");
$inarray = array_intersect($categories, $cats_iwant);
//this '$inarray' now has values like 'Branding', 'Design' which are in both arrays
if (count($inarray) > 0) {
//we have at least one common word ('Branding', ...)
}
//short version
if (count(array_intersect(get_field('categories'),array(
'Branding', 'Design', 'Print'
))) > 0)
{
//do stuff
}

Pull Content Regardless of whether user has posts or not

This post relates to WordPress and CIMY User Extra Fields. I do not think you need a knowledge of the latter to help with this problem, as it seems to be a WordPress issue more than anything.
CIMY User Extra Fields is a plugin that allows registered users to have much more information in their profiles. You can add as many fields as you want. You then have to edit "author.php" to pull in the new information.
I am currently using the following code to pull in the new user profile fields:
<?php if (have_posts()) { $flag = true; while (have_posts()) { the_post();
if ($flag) { $value = get_cimyFieldValue(get_the_author_ID(), 'dj-name');
if ($value != NULL) echo "<p><strong>Staff Bio: </strong>" . cimy_uef_sanitize_content($value);
echo "</p>";
$flag = false; }}}?>
The issue is this. Some of my users have 0 posts and this code will only pull the extra field content for the user if that have 1 post or more. This is due to the "if (have_posts())" function I suspect. Is there someway to modify the code to display the information even if the user has 0 posts?
Thanks
Zach
If it's not a need that a user must have a post to have CIMY values stored (which I assume), you just don't need to check for post-count > 0. You probably have copied that chunk of code over from a post template.
The following example just takes the value, and if it is set, will do the output via echo:
<?php
$authorID = get_the_author_meta('ID');
$value = get_cimyFieldValue($authorID, 'dj-name');
if (!empty($value))
echo '<p><strong>Staff Bio: </strong>'
, cimy_uef_sanitize_content($value)
, '</p>'
;
?>

Categories