简体   繁体   中英

how to break loop when value is not found in database

<?php

$str = get_the_ID(); //this is  getting id in wordpress index.php page
$value = 0;
$count = 0;
$sql1 = "SELECT * FROM wphg_mr_rating_item_entry where post_id='" . $str . "'";
$result1 = $wpdb->get_results($sql1) or die(mysql_error());

foreach ($result1 as $results1) {
    //if(!empty($results1->rating_item_entry_id)) {
    if (!$wpdb->show_errors($results1->rating_item_entry_id)) {

        $sql = "SELECT * FROM wphg_mr_rating_item_entry_value where rating_item_entry_id='" . $results1->rating_item_entry_id . "'";
        $result = $wpdb->get_results($sql) or die(mysql_error());

        foreach ($result as $results) {
            $sum = $results->value . "<br/>";
            $sum1 = $results->value . "<br/>";
            //$sum[]= array_push($sum,$value);   
            //echo count($sum+=$value);
            //print_r($value);  
            $count+=count($sum1);
            $value+= $sum;
        }
    } else {

        break;
    }
}


echo $value;
echo $count;
$value = $value / $count;
?>

when $str ="this is id " we are using to fetch data according to match. example : like your have id like = 26 where data is found in table this is working fine. but if here your post id data not found in database everything messed up.

please help me i want to here when data is available in database table run sucessfully . but if data not match according to id loop break and also continue for next id .

thnaks

<?php 
$str = get_the_ID() ;
 $value=0;
 $count=0;
 $sql1="SELECT * FROM wphg_mr_rating_item_entry where post_id='".$str."'";
   $sql2= $wpdb->get_row( $wpdb->prepare( "SELECT * FROM   wphg_mr_rating_item_entry where post_id= %s",$str)); // take second condition here  before reaching to loop and check your condition . 
   //echo $sql1->post_title; 
     if($sql2!=''){ // if value not equal to null then going for this logic working fine for me.

      $result1 = $wpdb->get_results($sql1) or die(mysql_error()); 
      //var_dump($result1); 
      foreach( $result1 as $results1 ){  
    //if(!empty($results1->rating_item_entry_id)) {
    //if(!$wpdb->show_errors($results1->rating_item_entry_id )) { 

      $sql="SELECT * FROM wphg_mr_rating_item_entry_value where rating_item_entry_id='".$results1->rating_item_entry_id."'";
        $result = $wpdb->get_results($sql) or die(mysql_error());

   foreach( $result as $results){
       $sum =  $results->value."<br/>";
       $sum1 =  $results->value."<br/>";
         //$sum[]= array_push($sum,$value);   
           //echo count($sum+=$value);
           //print_r($value);  
        $count+=count($sum1);
          $value+= $sum;
     //}
     }
     }



   //echo $value;
 //echo $count;
 $value=$value/$count;   
  }
?>  

take second condition here before reaching to loop and check your condition . if value not equal to null then going for this logic working fine for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM