【解決方法】ネイティブ PHP コードを codeigniter コードに変換する方法

プログラミングQA


$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>";}

私が試したこと:

<blockquote class="quote"><div class="op">Quote:</div>hey guys can you please help me convert this native php code to codeigniter 3.
</blockquote>

解決策 1

チャットGPT?

質問

このphpコードをcodeigniterに変換できますか

PHP
$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>";}

答え

もちろん! 以下は、特定の PHP コードに相当する CodeIgniter です。

PHP
$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>";
}

この CodeIgniter コードでは、入力ライブラリを使用して POST リクエストからコード値を取得します。 次に、CodeIgniter のアクティブ レコード ($this->db) を使用して、SQL クエリを作成および実行します。 get メソッドは結果オブジェクトを返します。これを使用して、クエリによって返された行数を確認できます。 最後に、返された行数に応じて、送信ボタンまたはエラー メッセージを出力します。

コメント

タイトルとURLをコピーしました