I would like to show success message if execute is successful. The below code doesn't show the message just deletes it. what am I missing?
$errors = array();
$delete = $mydb->prepare("update messages set deleted = 'yes' where to_user = ? and id = ? ");
$delete->bind_param('ss', $username->username, $id);
foreach ($_POST['id'] as $id) {
$delete->execute();
}
$errors[] = "Message Deleted.";
}
<div><?php
if ($delete->execute()) { echo $errors;}
?>
</div>
$errors seems to be an array so you will have to loop through it to echo errors which can be done by
foreach($errors as $value){
echo $value;
}
, but from the above code it seems you have only one value in array so you can use
echo $errors[0];
Also note you may need to be change this
if ($delete->execute()) { echo $errors;}
to
<?php
if (count($errors)>0) { echo $errors;}
?>
Thanks
Variable $errors is An array not a string and you can NOT echo array variable,
you have to use foreach($errors AS $v) echo $v;
Related
Im writing a page in HTML/PHP that connects to a Marina Database(boats,owners etc...) that takes a boat name chosen from a drop down list and then displays all the service that boat has had done on it.
here is my relevant code...
if(isset($_POST['form1'])){//if there was input data submitted
$form1 = $_POST['form1'];
$sql1 = 'select Status from ServiceRequest,MarinaSlip where MarinaSlip.SlipID = ServiceRequest.SlipID and BoatName = "'.$form1.'"';
$form1 = null;
$result1 = $conn->query($sql1);
$test = 0;
while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$values1[] = array(
'Status' => $row['Status']
);
$test = 1;
}
echo '<p>Service Done:</p><ol>';
if($test = 1){
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
echo '</ol>';
}else{
echo 'No service Done';
}
the issue im having is that some of the descriptions of sevice are simply Open which i do not want displayed as service done, or there is no service completed at all, which throws undefined variable: values1
how would I stop my script from adding Open to the values1 array and display a message that no work has been completed if values1 is empty?
Try this
$arr = array();
if (empty($arr))
{
echo'empty array';
}
We often use empty($array_name) to check whether it is empty or not
<?php
if(!empty($array_name))
{
//not empty
}
else
{
//empty
}
there is also another way we can double sure about is using count() function
if(count($array_name) > 0)
{
//not empty
}
else
{
//empty
}
?>
To make sure an array is empty you can use count() and empty() both. but count() is slightly slower than empty().count() returns the number of element present in an array.
$arr=array();
if(count($arr)==0){
//your code here
}
try this
if(isset($array_name) && !empty($array_name))
{
//not empty
}
You can try this-
if (empty($somelist)) {
// list is empty.
}
I often use empty($arr) to do it.
Try this instead:
if (!$values1) {
echo "No work has been completed";
} else {
//Do staffs here
}
I think what you need is to check if $values1 exists so try using isset() to do that and there is no need to use the $test var:
if(isset($values1))
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
Or try to define $values1 before the while:
$values1 = array();
then check if it's not empty:
if($values1 != '')
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
All you have to do is get the boolean value of
empty($array). It will return false if the array is empty.
You could use empty($varName) for multiple uses.
For more reference : http://php.net/manual/en/function.empty.php
This my code syntax all conditions are working but last else is not working. I found solution in there https://stackoverflow.com/a/5930255/7688968 and I am used break 2 but still not working. How can I solve that?
<?php
$rows = $wpdb->get_results( "SELECT * FROM test WHERE approve_status = '1'" );
if($rowcount>0)
{
foreach ($rows as $row)
{
if ( is_user_logged_in() )
{
echo 'I am user';
$demo = $wpdb->get_results("SELECT * FROM abc WHERE user_mail = '$curentmail'");
foreach($demo as $demos)
{
if(!empty($demos->id)){
echo 'I am IF';
break 2;
}
else{
echo 'I am last ELSE';
}
}
}
else{
echo 'I am guest user';
}
}
}
You are doing the else in the wrong place, it should be done instead of the foreach...
$demo = $wpdb->get_results("SELECT * FROM abc WHERE user_mail = '$curentmail'");
if(!empty($demo)){
foreach($demo as $demos)
{
echo 'I am IF';
}
}
else{
echo 'I am last ELSE';
}
So the logic is that if no records are returned then display your message.
Because the break doesn't break if's. You need to break the foreachs only once, so it's just break;
That else actually doesn't run, because the if is not false. If it uses break in the ifs, else won't run at all. The innermost else runs only, if the very first id of $demos is empty. The second only when if the user is not logged in. These don't relate too much for the foreach cycles.
Move whole foreach block in the function and return from it when needed. For example:
if($rowcount>0)
{
doStuff($rows);
}
function doStuff($rows) {
global $wpdb, $curentmail;
foreach ($rows as $row)
{
if ( is_user_logged_in() )
{
echo 'I am user';
$demo = $wpdb->get_results("SELECT * FROM abc WHERE user_mail = '$curentmail'");
foreach($demo as $demos)
{
if(!empty($demos->id)){
echo 'I am IF';
return;
}
else{
echo 'I am last ELSE';
}
}
}
else{
echo 'I am guest user';
}
}
}
Hello I have a simple script
This is my script, and try this script
<?php
$user_id = $_REQUEST['user_id'];
$pid = $_REQUEST['pid'];
$nopr = $_REQUEST['nopr'];
$tglpr = $_REQUEST['tglpr'];
$uraianpr = $_REQUEST['uraianpr'];
$jenispr = $_REQUEST['jenispr'];
$nilaipr = $_REQUEST['nilaipr'];
$costproject = $_REQUEST['costproject'];
$remarkpr = $_REQUEST['remarkpr'];
$tmpattachid = $_REQUEST['tmpattachid'];
if ($user_id==NULL)
{
$error "User Id Not Complete";
}
else if ($pid==NULL)
{
$error= "PID Not Complete";
}
echo $error;
?>
I want if $user_id or other variables is empty/null
i confused when other variable have empty/null data
i must typing code many if statement :(
example : 5 variables ($pid,$nopr,$tglpr,$jenispr,$costproject) is empty/null
this show error like this
PID Not Complete
NoPR Not Complete
Tgl Pr Not Complete
Jenis Pr Not Complete
Cost Project Not Complete
Help Me, Thank's.
I won't give you the full answer, as stated in comments, this is a basic basic concept you need to learn, but I will show you the template.
You want to use a FOREACH loop to check each value in the array. See http://php.net/manual/en/control-structures.foreach.php
And Set it out like this:
foreach($_REQUEST as $key=>$row){
if (is_null($row) || empty($row)){
$errorText .= "You have no data in your ".$key."<br>";
}
}
unset($key,$row);
Then you can output the $errorText value telling people which rows should be fixed. Easy.
Loop through the $_REQUEST and find the empty or null key.
CODE :
foreach ($_REQUEST as $key => $value) {
if(trim($value) ==='' || is_null($value))
{
echo $key . " is not complete" . "<br/>";
}
}
I'm trying to return a value to an android application in JSON format using a web service. For some reason, my code is only returning "Invalid login, please try again". I'm positive that the username is being entered, and I'm positive the tasks are returning properly. Using test code, which I've included as commented lines, I verify that the service will return one set of data to the application. It appears that the problem is with my foreach loop, but I don't see how. It's pretty simple stuff. Obviously it isn't iterating, or my count would be increasing and I wouldn't receive the Invalid Login error. The only thing I can figure is that I would need to use a nest foreach loop, but I haven't had much success with that, either.
Thanks in advance for the help, guys!
Here is the foreach loop...
//for reach task returned, add to array for later output
foreach ($row2['Description'] as $t)
{
$taskDisplay = array('task' => $t);
$taskCount++;
}
and the count...
if ($taskCount > 0)
{
echo json_encode($taskDisplay);
}
else
{
echo "Invalid login, please try again";
}
here is the code in full
$taskDisplay = array();
$taskCount = 0;
//do this portion if app gives key value "PART_ONE"
if ($result['part'] == "PART_ONE")
{
//if username was entered, check user credentials
if(array_key_exists('user', $result)) {
$usercheck = $con->prepare("Select * from Employee
Where usr = ?
and pass = ?");
$usercheck -> execute(array($result['user'], $result['password']));
$row = $usercheck -> fetch();
//if username was verified, select tasks from database
if($row['usr'])
{
$task = $con->prepare("Select * from Tasks
where Assigned_Employee = ?");
$task -> execute(array($result['user']));
$row2 = $task -> fetch();
$statusCode = "Success";
//$taskDisplay = $row2['Description'];
//for reach task returned, add to array for later output
foreach ($row2['Description'] as $t)
{
$taskDisplay = array('task' => $t);
$taskCount++;
}
}
else
{
$statusCode = "Fail";
//bail if invalid username entered
exit;
}
//$arr = array('task' => $taskDisplay, 'status_code' => $statusCode);
//echo json_encode($arr);
if ($taskCount > 0)
{
echo json_encode($taskDisplay);
}
else
{
echo "Invalid login, please try again";
}
}
}
To foreach over all rows you'll want fetchAll(). Then you want to access the Description column in each row:
$row2 = $task->fetchAll();
// etc...
foreach ($row2 as $t)
{
$taskDisplay[] = array('task' => $t['Description']);
$taskCount++;
}
My foreach loop:
$empty = "You cannot decommission this truck. It is in use."
$msg = "";
foreach ($trucks_to_remove as $truck_id) {
$truck_id = trim($truck_id);
if (strlen($truck_id)) {
$msg .= decom_truck(
$db,
$depot_id,
$truck_id
);
}
else
{
echo $empty;
}
}
echo $msg;
If the condition is not met, it means the sql cursor that preceded this foreach loop did not return a result for this truck_id (meaning it is in use), I just want a simple message sent to the page to alert the user.
It does not have to be a pop-up or the like. Where would I put my write or print statement? I assume I'd use an else clause but when I add an else inside the foreach loop (see above), I get nothing printed to the page. I have also tried the following:
else if (strlen($truck_id)) == 0
{
echo $empty;
}
I am very new to php.
$arr1 = array();
if(!$arr1)
{
echo 'arr1 is emty<br/>';
$arr2 = array(1, 2);
if($arr2)
echo 'arr2 has data';
exit();
}