ChatGPT?
Question
can you convert this php code into codeigniter
$code=$_REQUEST['code'];
$sql1="SELECT * FROM inventory WHERE code='$code' and available_quantity ='0'";
$result1 = mysqli_query($conn, $sql1);
if(mysqli_num_rows($result1) < 1){
echo "<button type='submit' name='submit'>Submit</button>";
}else{echo "<h5>Asset Quantity Exceeds Limit</h5>";}
Answer
Sure! Here's the CodeIgniter equivalent of the given PHP code:
$code = $this->input->post('code');
$this->db->where('code', $code);
$this->db->where('available_quantity', 0);
$result = $this->db->get('inventory');
if ($result->num_rows() < 1) {
echo "<button type='submit' name='submit'>Submit</button>";
} else {
echo "<h5>Asset Quantity Exceeds Limit</h5>";
}
In this CodeIgniter code, we use the input library to retrieve the code value from the POST request. Then we use CodeIgniter's active record ($this->db) to build and execute the SQL query. The get method returns a result object, which we can use to check the number of rows returned by the query. Finally, we output either a submit button or an error message depending on the number of rows returned.