Show null when data does not exist in database |
[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/>";?> <?php echo "<span class='title2'>". $profile_rows->ad_title ."</span>"."<br/>";?> <?php echo "<span class='title'>". $profile_rows->ad_description ."</span>"."<br/>";?> <?php echo "<span class='title2'>". $profile_rows->cat_type ."</span>"."<br/>";?> <?php echo "<span class='title2'>". $profile_rows->ad_name ."</span>"."<br/>";?> <?php echo "<span class='description'>". $profile_rows->ad_phone ."</span>"."<br/>";?> <?php echo "<span class='name'>". $profile_rows->ad_address ."</span>"."<br/>";?> <?php echo "<span class='phone'>". $profile_rows->ad_flag."</span>"."<br/>";?> <?php //echo "<span class='website'>". $row->address ."</span>"."<br/>";?> </span> <hr width="600px" /> <?php }?> How to show null data when data does not exist in database using codeigniter
[eluser]CroNiX[/eluser]
Maybe you need to read up on various join types. http://blog.codinghorror.com/a-visual-ex...sql-joins/
[eluser]InsiteFX[/eluser]
And wrap your code in code tags SEE: CroNix Signature or use POST REPLY for full editor
[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?
[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 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 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!
[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&i...zw&atsh;=1 PICTURE-2 https://mail.google.com/mail/u/0/?ui=2&i...zw&atsh;=1 i am desperately waiting for reply
[eluser]Tim Brownlaw[/eluser]
It appears that I cannot access those images! I get an error page. Did you try what I suggested above? |
Welcome Guest, Not a member yet? Register Sign In |