Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
There is a block of conditions.Help to shorten and improve this part of the code
$stock = $row['KOLVO_T'];
if ($row['CENA'] < 1000 && $row['KOLVO_T'] == 1) {
$stock = 0;
}
if ($row['CENA'] >= 1000 && $row['KOLVO_T'] == 1) {
$stock = $row['KOLVO_T'];
}
if ($row['KOLVO_T'] >= 2) {
$stock = $row['KOLVO_T'];
}
return $stock;
I don't really see the point, but here's the simplest I could do:
$stock = $row['KOLVO_T'];
if ($row['CENA'] < 1000 && $stock == 1) {
$stock = 0;
}
return $stock;
The last two conditional blocks are useless since they repeat an operation you already did before the first conditional block, and their condition can't be verified if the first is.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a dynamic input form, I want to check if the record exists, if no then it can not insert data
my controller
$user = Master::where('id_a','=',$request->get('id_a'))->where('id_b','=',$request->get('id_b'))->get();
if($user->isEmpty()){
// insert
}else{
//message "cannot input"
}
if insert one data, success.. but if insert array in my controller not check..
why in input array data always insert???
This is pseudo for only check id_b
$data = [1,2,3,4];
$data_a = [1,2,3,4];
$masters = Master::whereIn('id_b', $data)->whereIn('id_a', $data_a)->get();
foreach($data as $key => $value) {
$isExisted = false;
foreach ($masters as $master) {
if ($master->id_b == $value[$key] && $master->id_a == $data_a[$key])
{
$isExisted = true;
break;
}
}
if ( ! $isExisted) {
$master = new Banner();
$master->value = your_data;
$master->save();
}
}
You could use exists()
if(Master::where('id_a','=',$request->get('id_a'))->where('id_b','=',$request->get('id_b'))->exists()) {
do something
}
Also I would suggest you reduce the amount of in-line stuff you're doing, instead something like this:
$id_a = $request->get('id_a');
$id_b = $request->get('id_b');
if(Master::where('id_a','=', $id_a)->where('id_b','=',$id_b)->exists()) {
do something
}
If I understand correctly, you want to insert a Master if a given id_a and id_b doesn't already exists for one Master
If so, you could actually use firstOrCreate :
Master::firstOrCreate(
['id_a' => $request->get('id_a'), 'id_b' => $request->get('id_b')],
['yourcolumntocreate' => columnvalue, ...]
)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using $i string for limit in foreach loop.
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine'){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
}
if($menu_style == 'normal'){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}
I can't want to use LIMIT in SQL query.
And tell me when i use if($i == 5){ break; } then code display just 4
menu name
Tell me how is display menu name as my required.
Something like that?
$i = 0;
foreach($string as $menu){
$i++;
if($i == 6){
break;
}
$menu_name = $menu['name'];
$menu_style = $menu['style'];
// i want to show total 5 menu names.
if($menu_style == 'magazine' && $i <= 5){
// i have 5+ menus names (magazine style)
// butt here i want to show just one last magazine
echo $menu_name;
} else if($menu_style == 'normal' && $i <= 4){
// i have 5+ names menus (normal style)
// butt here i want to show 4 last normal style
echo $menu_name.'<br>';
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if ($subtotal1 != 0 or $subtotal2 != 0 or $subtotal3 != 0 or $subtotal4 != 0 )
{
echo $compno.' '.$subtotal1.' '.$subtotal2.' '.$subtotal3.' '.$subtotal4;
echo "<br>";
$info4 =array($compno, $total);
}
The if block is getting executed even if one of the values of the $subtotal(n) is 0.
Any idea why?
You need to change "or" to "and".
if ($subtotal1 != 0 and $subtotal2 != 0 and $subtotal3 != 0 and $subtotal4 != 0 )
Use && instead of or, cause your if condition will get executed even one of your value is non-zero.
Using or in if statement simply means if any of the condition is true, execute the code.
Using && means execute the if statement if all the conditions are true. Thanks it make it more clear
if ($subtotal1 != 0 && $subtotal2 != 0 && $subtotal3 != 0 && $subtotal4 != 0 )
{
echo $compno.' '.$subtotal1.' '.$subtotal2.' '.$subtotal3.' '.$subtotal4;
echo "<br>";
$info4 =array($compno, $total);
}
Try this
if($subtotal1 != 0){
}
elseif($subtotal2 != 0){
}elseif($subtotal3 != 0)
{
}
elseif($subtotal3 != 0)
{
}
else
{
echo $compno.' '.$subtotal1.' '.$subtotal2.' '.$subtotal3.' '.$subtotal4;
echo "<br>";
$info4 =array($compno, $total);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I tried to create a shopping basket in my project, but some problem to handle this.
My project basket must allow duplicate value, but my code auto duplicate last insert to array. Why does this happen, and how to solve this auto duplicate?
<?php
if(isset($_SESSION['basket'])){
$point = count($_SESSION['basket']);
echo " point = ".$point;
$value = end((array_keys($_SESSION['basket'])));
$value++;
}else{
$value = 0;
echo " point = 0";
}
if(isset($_GET['id'] , $_GET['meter'] , $_GET['color'])){
$id = $_GET['id'];
$color = $_GET['color'];
$meter = $_GET['meter'];
$selected_product = array($id , $color , $meter);
list($_SESSION['basket'][$value][0],$_SESSION['basket'][$value][1] , $_SESSION['basket'][$value][2]) = $selected_product;
echo "<pre>";
var_dump($_SESSION['basket']);
echo "</pre>";
}
?>
Though its not exactly clear from your question. To remove duplication in array use array_unique()
You also had a typo at
array($_SESSION['basket'][$value][0],$_SESSION['basket'][$value][1] , $_SESSION['basket'][$value][2]) = $selected_product;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to put a validation for when you get the same date, with the same time and same venue, it will not insert in the database. It will only insert in the database if 1 instance will return false.
$res_date = $_POST['res_date'];
$res_venue = $_POST['res_venue'];
$res_dur = $_POST['res_duration'];
$qryjay = mysql_query("SELECT * FROM tbl_reservation WHERE res_date='$res_date' AND res_dur = '$res_dur' AND res_venue = '$res_venue'");
if ((mysql_num_rows($qryjay) == $res_date) && ($qryjay == $res_dur) && ($qryjay) == $res_venue){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
Try to fix your code like this:
<?
$res_date = mysql_real_escape_string($_POST['res_date']);
$res_venue = mysql_real_escape_string$_POST['res_venue']);
$res_dur = mysql_real_escape_string$_POST['res_duration']);
$qryjay = mysql_query("SELECT count(*) as countNbr FROM tbl_reservation WHERE res_date='".$res_date."' AND res_dur = '".$res_dur."' AND res_venue = '".$res_venue."'");
$result = mysql_fetch_assoc($qryjay);
if ($result['countNbr'] > 0)){
echo "<script language=javascript>alert('".$res_venue." is not available on ".$res_dur." on ".$res_date."!')</script>";
}
?>