getting data from 2 different table with JOIN sql. Codeigniter - sql

I use codeigniter, and I need to get data from 2 different table. for now it returns data only from works_image table. how can I get data from both 2 table ?
thanks a lot!!
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "";
}

As long as you're doing a SELECT * (Why is this a bad idea?), you shouldn't need to specify tables with the call to select(). It will select all fields by default.
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
Should work fine.
Instead, what you really should be doing, is specifying exactly which fields you need:
$this->db->select('works_image.id, works_image.name, works_image.id_work, works.id, works.name'); // (or whichever fields you're interested in)
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
This way you can be sure that (a) you're not pulling unnecessary data from your DB, and (b) your code won't break if/when you modify your DB schema.

This post should answer your question: http://www.whypad.com/posts/codeigniter-activerecord-join-tip/178/
In short, you need to rewrite your select statement from
$this->db->select('works_image.*', 'works.*');
to this:
$this->db->select('works_image.*, works.*');
Note that this was the first result on Google for 'CodeIgniter join'. Try Googling your questions first. You can often get yourself a faster answer :)

You should be to write below code
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "
";
}
I think that you get your result

Related

With PHRETS v2, Can I Sort the Data Fields Alphabetically? (Had this when using V1)

This code works in terms of retrieving data:
<?php
date_default_timezone_set('America/Phoenix');
require_once("composer/vendor/autoload.php");
$config = new \PHRETS\Configuration;
$config->setLoginUrl('my_url')
->setUsername('my_user')
->setPassword('my_pass')
->setRetsVersion('1.7.2');
$rets = new \PHRETS\Session($config);
$connect = $rets->Login();
$system = $rets->GetSystemMetadata();
echo "Server Name: " . $system->getSystemDescription();
$property_classes = ['Property'];
foreach ($property_classes as $pc) {
// generate the DMQL query
$query = "(BedroomsTotal=1+),(MlsStatus=ACT,PND)";
$results = $rets->Search('Property', $pc, $query);
file_put_contents('MyFolder/Property_' . $pc . '.csv', $results->toCSV());
} //end for each property class
php?>
I would like to know how to sort the fields alphabetically, in order to keep fields in a predicable order, which could also be used in an SQL CREATE TABLE statement. I had this ability with v1.
I would also like to be able to loop through the data fields with a FOR EACH kind of statement, in order to create a customized field delimiter; a custom delimiter helps with avoiding import errors in cases where the delimiter also appears within the metadata, such as quotes and commas within remarks section.
Any help is much appreciated. :)

How to set function in HTML while outputing HTML with PHP

Sorry for little weird tittle, english is not my native language, and its little hard for me to express myself about this, so I hope i'll get clearer in explanation :)
I have this piece of code, to fetch all rows from MYSQL db, and when it fetch it out, i want to do some action on everyone of that row. (I want to display different PDF files stored in DB).
Take a look
include_once 'dbcon.php';
$query = mysql_query(" SELECT email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='' value= 'button' > <br />";
}
How to increment title of button name, on every row, so I can use it later to access it via POST method to display PDF?
I want something like this...
1st row : name='button1'
2nd row : name ='button2' etc etc..
When im outputing HTML with PHP it only allow me to read some variables, not some functions stored in quotation marks.
Please help guys! Thanks
You can end the string and concatenate it with an expression. In this case, I use $i++ which works like a counter. It returns the value of $i and then increments $i for the next iteration:
$i = 1;
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button" . $i++ . "' value= 'button' > <br />";
}
But 'row 1' doesn't always have to be the same row. Due to rows being added and removed you may get a different result, so 'button1' isn't a very good name. It would be better if you could use the name or id of the row. So if the person table has a numeric id as well, you could use that:
$query = mysql_query(" SELECT id, email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button{$row['id']}' value= 'button' > <br />";
}
As you can see here, I didn't concat, but instead embedded the array variable in the string. This works similar to normal variables, except you have to put braces around the variable. Of course, you could still concatenate the value as in the first example.
We use [] to create an array of the buttons
I'm not in a live environment so I can't test it.
Accessing via $_POST:
$size = sizeof($_POST['button']);
for($i = 0; $i < $size; $i++){echo $_POST['button'][$i];}
// Code
include_once 'dbcon.php';
$query = mysql_query(" SELECT email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button[]' value= 'button' > <br />";
}
$query = mysql_query(" SELECT id, email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . " ";
}
Yes, i've tried to increment with for loop, but no luck.. This is doing a job very well :)
Good idea. Thanks guys very much!

SQL - Can not SELECT data from a specific table

$rs77 = $connector->query("SELECT * FROM content WHERE topic='1' AND date='$date' ORDER BY cpc DESC LIMIT 4");
while ($rw77 = $connector->fetchArray($rs77))
{
switch($rw77['type']) {
case "vid":
?>.... This is just a little part of my code.
It worked ok, and in one moment (didn't any kind of changes in db or in code).I tried to display mysql_error. But it doesnt show me nothing.
$your_query = "SELECT * FROM content WHERE topic='1' AND date='$date' ORDER BY cpc DESC LIMIT 4";
$value = mysql_query($your_query) or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
What can be the problem? Thank you.
UPDATE: all other tables work nice. I can select data from them.
Here is my table:

Issue in making appear all database table field names

I have this code which i use in order to make appear all the names of a table of a database.
It used to work... but suddenly it won't make appear anything..
Could you please take a look?
I'm working with SQL.
$section = "SELECT * FROM forma";
$res = odbc_exec($connection, $section) or die(odbc_error());
$firstrow = false;
while ($row = odbc_fetch_array($res)){
if (!$firstrow) {
foreach ($row as $column => $value) {
echo "<label> " . $column . "</label>";
echo "<input type='checkbox' name='data[]' value='" . $column . "' /><br/><br/>";
}
$firstrow = true;
}
}
Thanks
This is a fairly nasty way of retrieving the column names for a table. What it is doing is reading all the data in the table, ignoring the result set and only using it for the column names. What is happening however is that the table is empty and so nothing is being returned at all.
You need to amend the query to look at the meta data rather than the table itself. You will need to rework it. This SQL will retrieve the column names for that table for you...
Select Columns.Name
From Sys.Columns
Where Object_Name(Columns.Object_id) = 'forma'
Order By Columns.Column_Id;
After that you will need to rejig your code to take advantage of it.

opencart getcategories function goes into infinite loop

I have an opencart shop with around 1300 categories and i'm not able to insert a new category or edit the existing category. I have installed pagination function, but it speeds up only category view. i did some debugging and found that the below function goes into an infinite loop once i click insert or edit inside the admin panel. Can anyone help me how to fix this
public function getCategories($parent_id = 0) {
$category_data = $this->cache->get('category.' . (int)$this->config->get('config_language_id') . '.' . (int)$parent_id);
if (!$category_data) {
$category_data = array();
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
foreach ($query->rows as $result) {
$category_data[] = array(
'category_id' => $result['category_id'],
'name' => $this->getPath($result['category_id'], $this->config->get('config_language_id')),
'status' => $result['status'],
'sort_order' => $result['sort_order']
);
$category_data = array_merge($category_data, $this->getCategories($result['category_id']));
}
$this->cache->set('category.' . (int)$this->config->get('config_language_id') . '.' . (int)$parent_id, $category_data);
}
return $category_data;
}
First Solution:
Do you also have Category Descriptions in database? If so, these so many descriptions being loaded by MySQL may be making burden over server. Try optimize the Query, select only required columns, you may change query as under:
$query = $this->db->query("SELECT c.category_id, cd.name FROM ...
Second Solution
You can do one more debug trick which once solved my problem. Add an echo statement:
echo $result['name'];
$category_data = array_merge($category_data, $this->getCategories($result['category_id']));` `
This way you'll get to know the exact location of the category where process gets hanged. There may be some problem with that particular category only.
Third Solution
Try changing
'name' => $this->getPath($result['category_id'], $this->config->get('config_language_id')),
To:
'name' => $result['name'],
Hope this helps.
I have a current but not permanent solution that worked for me... I dont know whether it's the right way to do, but it works for me.I changed the query to this
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c ORDER BY c.parent_id,c.sort_order");
And commented off
//$category_data = array_merge($category_data, $this->getCategories($result['category_id']));
This currently works for me, but i don't know whether it's the correct way or not. Thanks for the quick help everyone.