CodeIgniter Forums
Show null when data does not exist in database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Show null when data does not exist in database (/thread-61080.html)



Show null when data does not exist in database - El Forum - 09-13-2014

[eluser]aqifilyaskhan[/eluser]
I am joining two tables, and i want to show data when id of both is equal.
And when id is not equal so show nothing, but it is showing everything

Please Please Please Please Help me.
Thanks in advance.

This is my Controller

<?php

/**
*
*/
class profile extends CI_Controller
{

function __construct()
{
parent::__construct();
}

function index()
{
$this->load->model("profile_model");
$data['profile_records']=$this->profile_model->getAllRecords();

}

}

?>


This is my Model



<?php

/**
*
*/
class profile_model extends CI_Model
{

function __construct()
{
parent::__construct();
}

function getAllRecords()
{


$this->db->select('*');
$this->db->from('ads AS A');// I use aliasing make things joins easier
$this->db->join('membership AS C', 'C.id = A.ad_id', 'INNER');
$this->db->where('A.ad_id = C.ad_fk');

$q = $this->db->get("ads");
if($q->num_rows() > 0)
{
return $q->result();
}
return array();
}
}
?>

This is my views


<?php




foreach ($profile_records as $profile_rows)
{
?>

<span>


echo "<span class='title2'>". $profile_rows->ad_timestamp ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='title2'>". $profile_rows->ad_title ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='title'>". $profile_rows->ad_description ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='title2'>". $profile_rows->cat_type ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='title2'>". $profile_rows->ad_name ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='description'>". $profile_rows->ad_phone ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='name'>". $profile_rows->ad_address ."</span>"."<br/>";?&gt;
&lt;?php echo "<span class='phone'>". $profile_rows->ad_flag."</span>"."<br/>";?&gt;
&lt;?php //echo "<span class='website'>". $row->address ."</span>"."<br/>";?&gt;
</span>
<hr width="600px" />
&lt;?php }?&gt;





How to show null data when data does not exist in database using codeigniter


Show null when data does not exist in database - El Forum - 09-13-2014

[eluser]CroNiX[/eluser]
Maybe you need to read up on various join types.
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/


Show null when data does not exist in database - El Forum - 09-13-2014

[eluser]InsiteFX[/eluser]
And wrap your code in code tags SEE: CroNix Signature or use POST REPLY for full editor



Show null when data does not exist in database - El Forum - 09-13-2014

[eluser]aqifilyaskhan[/eluser]
@CroNiX i have no problem in join when i am showing data so it is also showing null contain data, i want to show nothing when id of both is not equal or id of one of them is missing


Show null when data does not exist in database - El Forum - 09-13-2014

[eluser]Tim Brownlaw[/eluser]
I'm having a hard time trying to figure out what you actually want from what you have already provided.

What are your table structures?

Can you give an example of what you are expecting?


Show null when data does not exist in database - El Forum - 09-13-2014

[eluser]Tim Brownlaw[/eluser]
I took your code - created a test controller - made up dummies of your two tables and tested the generated SQL... using echo $this->db->last_query();

So after tinkering with it I came up with this...


Code:
$this->db->from('ads as a');// I use aliasing make things joins easier
$this->db->join('membership as c', 'c.membership_id = a.ad_id', 'INNER');
$this->db->where('a.ad_id = c.ad_fk');
$q = $this->db->get();
Which creates
Code:
SELECT * FROM (`ads` as a) INNER JOIN `membership` as c ON `c`.`membership_id` = `a`.`ad_id` WHERE `a`.`ad_id` = c.ad_fk

What you have is generating this...
Code:
SELECT * FROM (`ads` AS A, `ads`) INNER JOIN `membership` AS C ON `C`.`membership_id` = `A`.`ad_id` WHERE `A`.`ad_id` = C.ad_fk
NOTE: I changed your membership.id to membership.membership_id so you can change that back to id.
Spot the difference! You are Selecting from (`ads` AS A, `ads`)

it always pays to examine what is actually being created and checking it!!!
With DB stuff - the ole echo $this->db->last_query() comes in very handy!




Show null when data does not exist in database - El Forum - 09-14-2014

[eluser]aqifilyaskhan[/eluser]
@Tim Borwnlaw I am joining two tables, and i want to show data when id of both is equal. And when id is not equal so show nothing, but it is showing everything

Kindly see this pic https://mail.google.com/mail/u/0/?ui=2&ik=0af4166144&view=att&th=148701dfebf005e5&attid=0.1&disp=emb&realattid=ii_i03dy0nq1_148701d3dd5a8fb4&zw&atsh;=1 PICTURE-2 https://mail.google.com/mail/u/0/?ui=2&ik=0af4166144&view=att&th=148701dfebf005e5&attid=0.2&disp=emb&realattid=ii_i03dy0mu0_148701d3dd5a8fb4&zw&atsh;=1 i am desperately waiting for reply


Show null when data does not exist in database - El Forum - 09-14-2014

[eluser]Tim Brownlaw[/eluser]
It appears that I cannot access those images! I get an error page.

Did you try what I suggested above?