Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

php - How to recreate this forloop to work many times?

I have a database with lots of courses in for example, breakfast_cereal, breakfast_toast, lunch_salad, lunch_main etc etc

Im using this to put the data they enter into a variable containing there results, for example:

breakfast_toast = Wholemeal;Brown;50/50;
and
breakfast_hotdrinks = Tea;Coffee;Milk;

This is the script im using to gather that information:

 if(!empty($_POST['toast_selection'])) {
    foreach($_POST['toast_selection'] as $toast) {
    $toast = trim($toast);
    if(!empty($toast)) {
      $toastchoices .= "$toast;";
    }
   }
 }

This works absolutely fine, But i have about a lot of entries to collect and if they add more to the database i want them to automatically gather.

I had a go at trying to think of a way but i just can't figure it out, This is what i tried:

All the inputs are called the same as the $course_menuname, for example breakfast_cereal[]

$query = "SELECT * FROM course";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{

$course_menuname = $row['course_menuname']; #E.G breakfast_cereal, breakfast_toast, lunch_main
$course_item = $row['course_jsname']; #E.G cereal, toast, jacketpotato
$postResults = $_POST[''.$course_menuname .''];

  if(!empty($postResults)) {
    echo $postResults."<br />";
      foreach($postResults as $course_item) {
        $course_item = trim($course_item);
        if(!empty($course_item)) {
          $course_item1 .= "$course_item;";   
        }
      }
  }
echo $course_item1."<br />";
}

This is what the end result should look for

 if(!empty($_POST['toast'])) {
   foreach($_POST['toast'] as $toast) {
    $toast = trim($toast);
    if(!empty($toast)) {
      $toastchoices .= "$toast;";
    }
   }
 }
 if(!empty($_POST['toastextra_selection'])) {
   #Gather Toast EXTRAs Choices
   foreach($_POST['toastextra_selection'] as $toastextra) {
    $toastextra = trim($toastextra);
    if(!empty($toastextra)) {
      $toastextrachoices .= "$toastextra;";
   }
  }
 }
 if(!empty($_POST['toastextra_selection'])) {
   #Gather Cold Drinks Choices
   foreach($_POST['colddrinks_selection'] as $colddrinks) {
    $colddrinks = trim($colddrinks);
    if(!empty($colddrinks)) {
      $colddrinkschoices .= "$colddrinks;";
    }
   }
 }
 if(!empty($_POST['hotdrinks_selection'])) {
   #Gather Hot Drinks Choices
   foreach($_POST['hotdrinks_selection'] as $hotdrinks) {
    $hotdrinks = trim($hotdrinks);
    if(!empty($hotdrinks)) {
      $hotdrinkschoices .= "$hotdrinks;";
    }
   }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all you should not use the mysql_* functions anymore. These functions are marked as deprecated. Alternativly you can use PDO or the mysqli_* functions.

I guess group change is what you need. Let 's start with a simple example.

try {
    $data = array();
    $pdo = new PDO(...);
    foreach ($pdo->query("SELECT * FROM course") as $row) {
        if (!isset($data[$row['course_menuname'])) {
            $data[$row['course_menuname']] = array();
        }

        if (!empty($row['course_item'] && in_array($row['course_item'], $_POST[$row['course_menuname'])) {
            $data[$row['course_menuname'][] = $row['course_item'];
        }
    }
} catch (PDOException $e) {
    // error handling
}

// Output all choices by menuname
foreach ($data as $key => $value) {
    echo "Choices for " . $key . "
";
    echo implode(";", $value) . "
";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...