I have been trying to figure out how to check if any of one column's items present in another column in Codeigniter....
I have Bike,Car,Bus in a column(Vehicles) in a table1...
I also have Bus,Helicopter,Ship,Car in a column(Interested) in table2..
How to check if any of table2 interested column's items present in table1 vehicles column...
I tried like this...
$query = $this->db->get('table2');
foreach($query->result() as $row)
{
$a = explode(',', $row->interested);
$this->db->where_in('Vehicles', $a);
$query = $this->db->get('table1');
foreach($query->result() as $row2)
{
echo $row2->ID;
}
}
Could it be done ?? Please Help me... Thanks in advance....
It is very hard to help you without seeing the tables and how you are relating one to the other (such as user_id).
What you should do is two queries. One query to get the data from table 1 of the vehicles you are interested in. Then run through the result set and create a simple array of those vehicles in php. Then do a second query using where_in to select all the rows that are in the chosen vehicles array.
Your method is bad because the number of db queries depends on the size of the result set of your first query. You might find your code trying to do hundreds of database queries.
If you show a diagram of your tables, I could write some example code for you.
Here are the docs for where_in: https://www.codeigniter.com/user_guide/database/query_builder.html#CI_DB_query_builder::where_in
Related
Hello guys I wonder if this query I wrote with laravel's syntax is actually make sense.
What I am trying to do: I got two tables. One is users table. Second is the dependent table for users. its called user's_thoughts. I want to pull the top best results of posts from users table. and this top results are based on how many likes that particular post had. This like column is in user's_thoughts table.
Here is what I have put together for this:
$results=DB::table('users')->join('user_thoughts', 'users.id', '=', 'user_thoughts.user_id')->select('users.post_id', 'users.post_title', 'users.posts', 'user_thought.like')-get();
Now, after this query which selects from both tables, I am going to see which post had the highest amount of likes.
$count = DB::table('user_thoughts')->count('likes');
$highest = DB::table('user_thoughts')->max('$count');
Now, here I would loop the results out.
$show = foreach ($highest as $cherrypicked => $topones) {
echo $topones;
}
Have I followed this correctly? What do you think?
Thanks.
I'm currently trying to write a simple ORM with PHP and mysql. I want the orm class to be able to work with joined tables.
So here's my problem, the following code shows how I map the data the query yields into an array.
public function execute_query($db_connection)
{
$query = '';
foreach($this->sql_query as $query_part)
$query .= $query_part;
$result = $db_connection->query($query);
while($row = $result->fetch_assoc())
{
array_push($this->m_Data, $row);
}
}
db_connection is a mysqli object.
sql_query contains all the different query parts (e.g. sql_query['join'] etc.).
m_Data is the array that contains the data read from the db.
My specific problem now is when I'm using a join statement in my query this function will just override fields with the same name in my m_Data array. Also if I dont save the name's of the table the specific field data is coming from, I later can't update the tables with the same join statement.
tl,dr. I need to be able to not only save the table data like this: m_Data{ 'field_name' => 'value' } but I also need to save the table name the field is selected from. I could then save the data like this m_Data{ 'table_name.field_name' => 'value' } which enables me later to generate a query to update the joined tables successfully.
I cant seem to find any information on how to get the origin table name for each field I pull out of the result.
If it isnt possible with mysqli I'd much appreciate it if you point me in the right direction.
extra short problem statement:
I need to get a result set and read each row seperatly. For each row I need the following information for every field selected: field_name, table_name, value.
There must be a simple answer to this but I seem to be searching for the wrong keywords to find a solution.
I hope I've written this understandable enough.
Seems to me that you should store table column values in an object, so if you have a related table, the column values would be stored in a separate object - and so would not interfere with the values in your primary table.
In general you might work with the ORM this way:
// Make joined query
$rows = ...
foreach($rows as $row)
{
// $row just refers to the primary table
echo $row->id;
// You get a many:1 related table this way
echo $row->getRelatedRow()->value;
// You get a 1:many rows this way
$rows = $row->getOtherRelatedRows();
}
Depending on how you set up your query options, getting related data may or may not initiate further SELECTs to get the required data.
mysqli_result::fetch_fields has useful things:
http://www.php.net/manual/en/mysqli-result.fetch-fields.php
table
orgtable
field type
etc
I have three db tables: species, locations, records.
I have nested "while" statements that first loop through the locations to provide table headers. Further nesting accomplishes going through the records page to pull out each record and provide a total count of each species at each location.
I need to add an additional row underneath the last species to provide a count that totals each location.
This cannot be hard-coded since the number of species and locations can vary. Code is already setup to expand the table in both rows and columns. All I need to figure out is how to do the simple calculation of each location's (column) total.
I was thinking I need to do this in some type of an array to load it but I cannot find any real documentation on how I want to do this (at least that I can understand).
you have to use a UNION on this table.
you can create a temporary table first to do this then make a select from it.
roughly:
your select.....
UNION
select 'Total',sum(location1),sum(location2),sum(location3) from
(your select) as b
Total granny way to do it, but you said you needed something you understand, and I'll bet joins and unions are a little out of your scope. This may not be syntactically correct, but it should get you closer.
Removed MySQL | No longer applicable
or you can do it in php:
<?php
$Results = mysql_fetch_assoc("Select * FROM table");
$locArr = new Array();
foreach($R in $Results){
foreach($prop in $R){
$locArr[$prop] += $R[$prop];
}
}
foreach($prop in $locArr){
echo $prop."Total: ".$locArr[$prop]."<br/>";
}
?>
function main2(){
$sql = mysql_query("select * from clubs,venue where id<>".$_SESSION['userid']);
$cnt = 0;
while($row = mysql_fetch_array($sql)like
<td width="19%"><?php echo $row['club_name'];?></td>
when I use this code, my entry will be double show to me. What wrong with this like club name will be show twice to me.
Probably because you don't JOIN the tables in your SQL query so the engine doesn't know how the data are related.
You're doing a JOIN on the clubs and venue table. Depending on how your rows are saved, it'll give you all the possible matches. If you have a specific result in mind, please explain it and we'll need your database schema (the structure of your DB) as well.
I was wondering if anyone knew how to get the metadata info from a Zend_Db_Table_Rowset class when using joins in the query that produced that result set? It's easy when there are no joins involved.. for example:
foreach ($rowset as $row) {
$info = $row->getTable()->info(Zend_Db_Table_Abstract::METADATA);
Zend_Debug::dump($info); // outputs array of column info including data type
}
But when I do that to a row that came from a query using joins I just get the data from the main table I was selecting from..
Jose, what kind of metadada info do you need? Maybe there is an alternate way to achieve what you want. Since the joins are always done from one table (and you join it to others) I think you will always get the metadata info for the first one.
By any chance are you doing dynamic joins?