I'm having an issue with creating custom fields after data has already been added to the DB. I've been looking at this for a while and my brain has shut down. I'll try and explain the best I can.
I have 2 tables, custom_fields and custom_fields_values. I also sort these fields via a table column. When I pull them from the db I have to join the tables in order to have the results show in the same order as the fields. If I don't then data gets mixed up.
$customfield = $pdo->prepare("SELECT * FROM custom_fields ORDER BY fieldorder ASC");
$customfield->execute();
$customfield = $customfield->fetchAll(PDO::FETCH_ASSOC);
$customfieldvals = $pdo->prepare("SELECT vals.* FROM custom_fields_values vals JOIN custom_fields fields ON fields.id = vals.fieldid WHERE vals.related_system=:relsystem ORDER BY fields.fieldorder ASC");
$customfieldvals->bindParam(':relsystem', $get_system['id'], PDO::PARAM_STR);
$customfieldvals->execute();
$customfieldvals = $customfieldvals->fetchAll(PDO::FETCH_ASSOC);
I then have a foreach loop that does properly display the data. The issue is, when I add a new custom field that does not have a value because it was created in the custom_fields table, but NOT in the custom_fields_values table so it doesn't match at ON fields.id = vals.fieldid. If I resort the fields while this has no value, it takes the value of another field. Essentially, because it doesn't match at the id level of fields and fieldid of values, everything gets messed up.
How can I make it to where if it does not have a value row created, it will just show NULL no matter what instead of taking the value of another field?
EDIT
Am I going about this the right way or is there a better way to have the custom field value follow the custom field rather than the JOIN method?
You have to use Left Join in to your query. Left Join will return null If it does not found value for custom field into custom_fields_values table.
So Query will look like this.
$customfieldvals = $pdo->prepare("SELECT vals.* FROM custom_fields_values vals LEFT JOIN custom_fields fields ON fields.id = vals.fieldid WHERE vals.related_system=:relsystem ORDER BY fields.fieldorder ASC");
I didn't think about this initially, but instead of calling a separate query for just values I totally removed the $customfieldvals query and changed the $customfield query. This instead locks the value to the field rather than just linking them.
$customfield = $pdo->prepare("SELECT * FROM custom_fields fields LEFT JOIN custom_fields_values vals ON fields.id = vals.fieldid WHERE vals.related_system=:relsystem ORDER BY fieldorder ASC");
$customfields->bindParam(':relsystem', $_GET['id'], PDO::PARAM_STR);
$customfield->execute();
$customfield = $customfield->fetchAll(PDO::FETCH_ASSOC);
and then my foreach:
foreach ($customfield as $fields) {...
This allowed me to call
$fields['value']
rather than
$customfieldvals[$key]['value']
Related
I have been trying everything to achieve this task, but it is rather difficult.
I am trying to do this in Wordpress as shortcode.
The shortcode itself is fine, I don't need any help with that, but just the output here which is giving me a difficult time. The Shortcode has an attribute that can be used to show the ID of the list using $listID
As an example, I have 3 database tables.
complete (variables: id, itemID, listID, userID)
item (variables: id, listID, description, creator)
list (id, name)
What I need to do is show a list from a shortcode variable, in this case database id 1 for the list. Then show the items in that list (using listID in item variable as well as wordpress user function to grab the ID of the logged in user and compare it to the creator variable of the item) -- and have the completed marked in there (using itemID, listID and the logged in user to wordpress to compare with the userID).
I tried this for SQL, but it returned nothing
global $wpdb;
$q_checked = $wpdb->prefix.'checked';
$q_item = $wpdb->prefix.'items';
$q_list = $wpdb->prefix.'list';
$q_results = $wpdb->get_results("
SELECT * FROM (($q_item INNER JOIN $q_list ON $q_item.listID = $listID)
INNER JOIN $q_checked ON $q_list.id = $q_checked.listID;
I also tried this with sql but it only shows from the two tables and not the third, and it will show all instead of excluding the completed.
$q_items = $wpdb->prefix.'items';
$q_checked = $wpdb->prefix.'checked';
$q_result = $wpdb->get_results("SELECT $q_items.title, $q_checked.userID FROM $q_items INNER JOIN $q_checked ON $new_items.id = $q_checked.itemID");
I thought about using a foreach, but none of the above would work well with that would it? Since you can only use one result. Maybe if I could do 2 separate foreach, 1 for the items in the list but exclude them if the id of the item matches the itemID in the completed database? Then do another foreach that would show the completed items for that list.
<?php foreach($q_result as $i ) {
$userID = $i->userID;
$itemNAME = $i->title; ?>
<?php if($userID === ''.$current_user->ID.'') { ?> <?php echo $itemNAME; ?><?php }?>
<?php }; ?>
I honestly think I should rethink the entire thing. Maybe I am overcomplicating it?
This is a fairly standard MySQL query:
SELECT i.description, i.creator,
IF(c.id IS NOT NULL, 'Completed', 'Not Completed') AS status
FROM item i
LEFT JOIN complete c
ON c.itemID = i.id AND
c.listID = i.listID AND
c.userID = ?
WHERE i.listID = ?
The ? placeholders represent parameters that you would bind to the current user ID and list ID supplied by your shortcode.
This performs a left join from items to completed items, and checks for a match on all join conditions. If a match is found, then a row will exist in c, and we mark the item as completed. Otherwise, the c.id will be NULL and we mark it as not completed.
I have a query a Wordpress include which does the following:
$sql = "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'merchant_id' LIMIT 6";
$results = $wpdb->get_results($sql);
foreach ($results as $row)
{
echo $row->meta_value . ",";
//sprintf("SELECT * FROM retailers WHERE advertiserId = '%s'", $row->meta_value);
}
//clean all results
$wpdb->flush();
It parses all pages custom fields (merchant ID numbers), and returns any unique distinct values, separated by a comma. This bit works great.
ie: The above may return: 1301,345,723,134,1435
What I also have is a separate MySQL table of about 20 fields, three of which are the MerchantID, programmeName, and commissionMax. Each value from the CSV correlates with the MerchantID in the database.
Regardless of the amount of merchant ID's that appear in the CSV - I need to parse them all, but show the three highest commission rates (commissionMax), as well as the programmeName.
I managed to get connected to my external database and retrieve the appropriate values (using the commented code above) however this showed all of the retailers information.
Any advice?
Use the following query with limit:
SELECT * // select all fields
FROM table_name // from your table
WHERE MerchantID IN (your_ids_here) // IDs received from previous query or wherever
ORDER BY commissionMax DESC // descending sort by commissionMax field
LIMIT 3 // take first 3 results
I have two tables 'accounts_transactions' and 'accounts_bills_transactions'.
I have to left join these two using active record of codeigniter.But the names of key columns used to join are different.So I am not getting the key column from the left table in the output .What query should I write to get the key column from the left table included in the result.
My code is
$this->db->select('*');
$this->db->from('accounts_transactions');
$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_bills_transactions.transaction_id','left');
$query = $this->db->get();
So, as you see the key columns used to join here are , id from left table and transaction_id from second table.The problem is that I am not getting the id from left table in the result.But I am getting all other columns.I assume the problem is because of difference in column names used to join.ie both the column names are not named 'id' .So how can I get the id from left table included in the result.
You could alias them:
$this->db->select('accounts_transatctions.*, account_transactions.id AS a_id,
accounts_bills_transactions.*,
account_bills_transactions.id AS ab_id');
$this->db->from('accounts_transactions');
$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_transactions.transaction_id','left');
$query = $this->db->get();
The two IDs will now be available as a_id and ab_id (or whatever alias you choose)
Note: I'm not sure if you can alias in AR without avoiding escaping (haven't been using CI for a while). Should you get any error for that reason, just pass false as second parameter of $this->db->select():
$this->db->select('...', false);
you can try this if you confuse of using $this->where or $this->join
$query = $this->db->query("select ......");
return $query;
You problem is so simple. You can use this query
$query = $this->db
->select('at.*')
->select('abt.id as abt_id');
->from('accounts_transactions at');
->join('accounts_bills_transactions abt', 'at.id = abt.transaction_id','left');
->get()
->result();
When same column are used in join it selects only one. You need to give alise to the other column in second table. The best practice is to use a structure like this
accounts_transatctions
--------------------------
accounts_transatctions_id
other_columns
accounts_bills_transactions
---------------------------
accounts_bills_transactions_id
accounts_transatctions_id
other_columns
Assume, I have the Following Tables in my Database.
POSTS:
id, title, content
CATEGORIES:
id, name
RELATION:
post_id, cat_id
I have been succesfully able to insert the values into these tables, while publishing the post.
I am also, able to display the categories beneath each and every post on my Homepage. But, I Fear the method I am using is very resource intensive.
Here is What I Do in MySQL/PHP.
(You can choose to skip the code, and read its description for better understanding)
//Get all the posts
$database = $connection->prepare("Select * FROM POSTS");
$database->execute(array());
$result = $database->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $r) {
$articleid = $r['id'];
//Display Post title and other things here
//Get the Category Ids
$database = $connection->prepare("Select * FROM RELATION WHERE post_id = ".$article);
$database->execute(array());
$ret_cat= $database->fetchAll(PDO::FETCH_ASSOC);
//Get names of category, and dislay them
foreach($ret_cat as $rc)
{
$catid = $rc['cat_id'];
$database = $connection->prepare("Select * FROM CATEGORIES WHERE cat_id= ".$catid);
$database->execute(array());
$ret_cat_name= $database->fetch(PDO::FETCH_ASSOC);
echo "<a href='index.php?category=".$rc['cat_id']."'>".$ret_cat_name['name']."</a> ";
}
}//End First ForEach
Select All the the Desired Posts from the POSTS table.
Use a Foreach loop to display each post.
Within the foreach loop, I use another SELECT statement to get the categories from RELATION table by matching the POSTS.id.
Then, I Use another foreach loop to display all the categories.
But, since I have only the category id in the RELATION table and I need to display the name of the Category instead.
So, I use another SELECT statement to get the name of the Category from the CATEGORIES table, by using RELATION.cat_id.
The Code Works Fine, and I get what I want. But, I feel there are a lot of mysql requests being generated. Hence, load would be increased on server.
So, I need to know what I am doing is right or not. Or, if there is a simpler way to do all this?
Why not use JOIN to decrease the number of database round-trips?
For example, first select the desired posts...
SELECT * FROM POSTS WHERE <some filter criteria for posts>
...and then for each returned row...
SELECT CATEGORIES.*
FROM RELATION JOIN CATEGORIES
ON RELATION.cat_id = CATEGORIES.id
WHERE
post_id = <value from the first query>
Or even do everything in a single round-trip...
SELECT POSTS.*, cat_id, name
FROM POSTS
JOIN RELATION
ON POSTS.id = RELATION.post_id
JOIN CATEGORIES
ON RELATION.cat_id = CATEGORIES.id
WHERE
<some filter criteria for posts>
...in which case the client code would need to "merge" the resulting rows that have the same POSTS.id.
BTW, you should almost never do something like...
Select * FROM POSTS
...without a WHERE clause. This simply won't scale as the database grows.
HI all,
I am trying to figure out how to put this into words even, but I am wanting to know how to format the output from each table separately in a "multiple table" mysql query. The output from the table1 "wall" is formatted within a while loop, but the content from table2 "actions" is already formatted(as 1 line of text with links) before it is inserted into the table(column action_body), so inside the loop I would only be outputting the action_date and action_body columns from the actions table.
I am probably not using the correct sql method(if Im doing anything right at all, that is) for the results I need, so feel free to correct my novice example, or suggest a new way to approach this.
Query:
$query = "SELECT wall.wall_id, wall.wall_owner_id, wall.wall_user_id,
wall.wall_post_date, wall.wall_post_content, actions.action_id,
actions.action_date, actions.action_user_id, actions.action_title,
actions.action_body FROM wall, actions
ORDER BY wall.wall_post_date, actions.action_date DESC";
$result = mysql_query($query);
while( $rows = mysql_fetch_assoc($result) {
// What to put here
}
Any help is appreciated, thanks, Lea
Update after comments
SELECT w.* FROM (
(SELECT
'w' as type,
wall_id as id,
wall_owner_id as owner_id,
wall_user_id as user_id,
wall_post_date as post_date,
NULL as title,
wall_post_content as content
FROM wall
WHERE wall_owner_id = x # user id of owner
)
UNION
(SELECT
'a' as type,
action_id as id,
action_user_id as owner_id,
NULL as user_id,
action_post_date as post_date,
action_title as title,
action_body as content
FROM actions
WHERE action_user_id = x # user id of owner
)
) w
ORDER BY w.post_date DESC
Because you don't JOIN on a specific field, you're gonna get every row-row combination of the two tables, which is a whole lot more data than you probably want.
You'd be better of by doing 2 queries, one for each table. While looping through the result of each table, you can collect the data you want in one array, with the field you want to sort it by as array key.
Then you sort the array, and loop through it to print it out.