I have this piece of code
<?php for ($i=0;$i<sizeof($list["tags"]); $i++) {
if ($list["tags"][$i]["title"]=='list') {
echo 'Not correct type';
}
if ($list["tags"][$i]["title"]!='list') {
?>
Text
<?php }
}
?>
My problem is that when $list["tags"][$i]["title"]=='list', I get the message 'Not correct type' many times as the loop continues. How can I echo that message only once?
You can insert break; after the echo statement to exit the loop when the condition is met. Use break n; to exit out of n layers of loops/conditionals.
You'll just have to keep track of whether you've already shown it or not:
$shown = false;
for ( $i = 0; $i < sizeof( $list['tags'] ); $i++ ) {
if ( $list['tags'][$i]['title'] == "list" && !$shown ) {
echo "Not correct type";
$shown = true;
}
if ( $list['tags'][$i]['title'] != "list" ) {
echo 'Text';
}
}
But this raises the question: why would you only want the message to show once? Wouldn't you want it to display "Not correct type" for all values of $i for which the title is not "list"?
Related
I want to check if a certain field of all rows match a criteria.
So if all rows has in the Status field 'RUN' as value then echo "Success".
If there is one row with END as value then echo "Fail":
I'm guessing I need a loop and an IF statement ?
I was thinking something like this but it doesnt return anything:
while($source_row = mysqli_fetch_array($source_selection)){
if ( ($source_row['Stat']) == ("Run" ) {
echo "Success<br />";
} else
echo "Fail";
}
I don't want to echo each row, I want all rows to match a criteria then echo, if one doesn't match a criteria then echo as well.
This should work for you:
First of all you have to fetch all rows with mysqli_fetch_all(). After this I extract only the Stat column with array_column(). Then I just simply array_fill() an array with X values as you have in $states with the value "Run". And check if every value is equals.
$result = mysqli_fetch_all($source_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else {
echo "Fail";
}
There are some error in the code and you can use the count to match -
$count = 0;
while($source_row = mysqli_fetch_array($source_selection)){
if ($source_row['Stat'] == "Run" ) {
$count++;
}
}
if($count != mysqli_num_rows($source_selection)) {
echo "Fail";
} else echo "Success";
For best performance you can do it directy on the SQL:
select count(*) from table where Stat <> 'Run';
And then test the returning value to check that is greater than zero.
To do it with php you should know that when you find an error you can stop the iterations. The code would look like that:
while($source_row = mysqli_fetch_array($source_selection)){
if ( $source_row['Stat'] != "Run" ){
$fail = true;
break;
}
}
if ($fail) {
echo "Fail";
} else
echo "Success";
}
Try this
$success=true;
while($source_row = mysqli_fetch_array($source_selection)){
if ( ($source_row['Stat']) != ("Run" ) {
$success=false;
}
}
if ($success) {
echo "Success";
} else {
echo "Fail";
}
This code helps me show the data stored in the reviews array but whenever i try calling the variables individually i get an error. So how can i display the variables indiviually.
<?php $reviews = Mage::getModel('review/review')->getResourceCollection();
$reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes()
->load();
print_r($reviews->getData());
This is line where i get the error:
echo $reviews->getTitle();
?>
the error is Fatal error: Call to undefined function. Please help.
I have never used Magento, but I think you have to iterate through reviews. Try this.
<?php
$reviews = Mage::getModel('review/review')->getResourceCollection()
->addStoreFilter( Mage::app()->getStore()->getId() )
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes();
if (count($reviews) > 0) {
foreach ($reviews->getItems() as $review) {
// Individual review
echo $review->getTitle();
}
}
?>
$ReviewsCollection = $this->getReviewsCollection()->getItems();
if( count( $ReviewsCollection ) ) {
$x = 1;
foreach( $ReviewsCollection as $_review ){
// get review title
echo $_review->getTitle();
$_votes = $_review->getRatingVotes();
if (count($_votes)){
foreach ($_votes as $_vote){
echo $_vote->getRatingCode().' - '.$_vote->getPercent().'%<br />';
}
}
// get review date
echo $this->formatDate($_review->getCreatedAt()).'<br />';
// get review detail
echo $_review->getDetail().'<br />';
// get how wrote the review
echo $_review->getNickname().'<br />';
// because you cant setPageSize with this collection it's best to break the loop.
if($x == 4){ break; }
$x++;
}
}
I have loop using for which results in some values:
$value1, $value2, $value3, $value4, ...
How can I make a calculation or relation between these values?
For example, if any two values < 40 echo something?
I hope that some code will make the idea clear:
<?php
$value=$_POST['name'];
for($i=1;$i<=$value;$i++)
{
$value{$i}=$_POST['name{$i}'];
}
if($value{$i} < 40)
{
echo "you failed";
}
else
{
echo "you succeeded";
}
?>
I want to show the message "you failed" if two values are < 40.
How can I do that?
I'm struggling to make sense of what your current code is actually doing, but that could just be that my PHP is a bit rusty. Either way, it sounds like the general pattern of what you're trying to do is to keep a count of fails and successes. As an overall structure (that is, not to be copied/pasted as-is since I don't know what your $value actually is here), it might look something like this:
$fails = 0;
$successes = 0;
for ($i = 0; $i < len($values); $i++)
{
if ($values[$i] < 40)
{
$fails++;
}
else
{
$successes++;
}
}
At this point $fails contains the count of values less than 40 and $successes contains the count of values greater than 40. Then you invoke your business logic for determining the overall result:
if ($fails >= 2)
{
echo "you failed";
}
else
{
echo "you succeeded";
}
To answer your question you can use the foreach statement
$value=$_POST['name'];
$hasValue = 0;
foreach($value as $key => $dat){ // since we have the value from $_POST we use foreach statement to loop it
if($dat < 40){ // check if the data in the list is less than 40
$hasValue++; // increment it if is.
}
}
echo ($hasValue > 2)?#"You failed":#"Success"; // check if it has more than 2 less than 40 values.
something like the above
and try to read about PHP
btw haven't code much in php lately so apologies for some syntax
As soon as any two values add to less than 40, the "failed" message shows and the loops are exited. If not, the success message shows.
$x = false;
foreach ($value as $k1 => $v1) {
foreach ($value as $k2 => $v2) {
if (($v1 + $v2) < 40 && ($k1 != $k2)) {
echo "you failed";
$x = true;
break (2);
}
}
}
if (!$x) echo "you succeeded";
I have a code like this
First looping count how many post the array:
for($i = 0; $i < $jumlah_qty ;$i++) {
if(!empty($qty[$i]) && !empty($id_cat[$i])) {
Insert booking:
$insert_booking_hd = $user_class->select($az);
$id_cates = $id_cat[$i];
for($b = 0;$b<$qty[$i];$b++) {
First validation if $_POST[$id_cates) is set run this code:
if(isset($_POST[$id_cates."".$b])){
$id_seat = $_POST[$id_cates."".$b];
Find the seat number in $select_seat and find if seat number is exist in $seat_number:
$select_seat = $user_class->select($query);
$seat_number = $user_class->select($querys);
$row_seat = $user_class->numrows($select_seat);
$row_seat2 = $user_class->numrows($seat_number);
if($row_seat>0) {
$update_seat = $user_class->update($update_false);
$bol[$b] = FALSE;
} else {
if( $row_seat2>0 ) {
$insert_booking_dt = $user_class->insert($insert);
$update_seat = $user_class->update($update_true);
$bol[$b] = TRUE;
} else {
$bol[$b] = FALSE;
}
}
} else {
$insert_booking_dt = $user_class->insert($insert_without_seat);
$bol[$b] = TRUE;
}
if($bol[$b]) {
echo "FALSE";
header("location:../../../print.php?id=$id_booking");
}
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
}
}
}
Anything wrong with my php validation?
Because if I input array of $id_seat it will always redirect to print.php although validation is FALSE
for example if I input 3 array and then I echo FALSE WRONG FALSE FALSE
still redirect to print.php not to event.php
How can I read if one of array is get WRONG and then redirect to event.php?
How can I read if one of array is get WRONG and then redirect to event.php?
You may break out of for-loops.
Instead of:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
You could try:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
break 2;
}
So I have the following code:
$colors=$ex->Get_Color("images/avatarimage3.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
foreach ( $colors as $hex => $count )
{
if ($hex == 'e6af23' && $count > 0.05)
{
echo "The image has the correct colour";
}
else
{
echo "The image doesn't have the correct colour";
}
}
Basically this code at the moment grabs the hex value and percentage of colours than image contains and adds them to an array. The code above looks to see if the hex is a certain value and the percent above 5% and if it is then it displays the success message. This part works exactly as it should do!
Now, what I also want is that if the colour isn't correct, so for all other hex values in the array other than $hex == 'e6af23' I want it to display a failure message but only display it once and not for every time the hex isn't that value.
Basically I need it so that the failure message is only displayed once and not 5 times (the number of hex colours in the image).
You can use a flag to indicate if the message has been outputted or not, and if so then don't output again:
$colors=$ex->Get_Color("images/avatarimage3.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
$error_displayed = false;
foreach ( $colors as $hex => $count ) {
if ($hex == 'e6af23' && $count > 0.05) {
echo "The image has the correct colour";
} else if (!$error_displayed) {
echo "The image doesn't have the correct colour";
$error_displayed = true;
}
}
Just keep a list of colors you already echoed.
$failed = array();
forech ($colors as $hex) {
if (!in_array($hex, $failed) && $error) {
echo 'Failed at hex ' . $hex;
$failed[] = $hex;
}
}
Using NewFurnitureRay's answer as a guideline I came up with this answer:
$colors=$ex->Get_Color("images/avatarimage.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
$success = true;
foreach ( $colors as $hex => $count ) {
if ($hex !== 'e6af23') {$success = false; }
if ($hex == 'e6af23' && $count > 0.05) {$success = true; break;}
}
if ($success) { echo "Success"; } else { echo "This is a failure"; }
Seems to work now as it should only displaying either a success or a failure regardless of the success's position in the array :)