I have code in PHP which works fine. I need to convert it to codeigniter code. I was trying to convert it code, but it displays an error. Please guide me.
Here is my code:
<?php
//$qry = mysqli_query("SELECT DISTINCT business_name FROM bg_forms");
$qry = $this->db->query("SELECT DISTINCT business_name FROM bg_forms");
while($row = mysqli_fetch_row($qry)){
//loo1
foreach($row as $value){
echo '<font color="#FF0000"><b> '.$value2.'</b></font><br>';
//loo2
$qry2 = mysqli_query("SELECT DISTINCT category_name FROM bg_forms WHERE business_name='".$value."'");
while($row1 = mysqli_fetch_row($qry2)){
foreach($row1 as $value2){
echo '<font color="#00CC00"><b> '. $value2.'</b></font><br>';
//loo3
$qry3 = mysqli_query("SELECT form_name, controller, php_file_name FROM bg_forms WHERE business_name='".$value."' AND category_name='".$value2."'");
while($row2 = mysqli_fetch_row($qry3)){
echo '<font color="#009900"> <a href="'.$row2['1'].'/'.$row2['2'].'.php">'.$row2['0'].'</a></font><br>';
}
}
}
}
}
?>
Looks like you are taking your first steps with CodeIgniter. You are welcome.
Thing is you are mixing two ways of working there. First you are using
$this->db->query()
which is a CodeIgniter construct. But then you are trying to process the results like you do with MySQLi. That won't work.
So, you already know how to generate queries. Now you have to learn how to work with the results:
https://www.codeigniter.com/userguide2/database/results.html
As you can see in the user guide qry is an object that has many methods to work with the results. The easiest and more straightforward one is result()
foreach ($qry->result() as $row){
echo $row->title;
echo $row->name;
echo $row->body;
}
Result() returns an array of objects or an empty array on failure. Read the docs in the link I pasted above and you'll learn about working with the query object.