-1

I am facing a problem while calculating the quotation amount for my photography project.


Assume an Indian wedding has many events like Engagement, Haldi,Marriage, Reception etc., Pricing will be determined based on the number of events selected. A user can select his desired events and submit the quote

Sample screenshot

enter image description here

The values of the selected events are stored in enq_event column in mysql using implode function

$checkbox2_selected = implode( "," , $_POST['wed_enq_events']);

sample data stored in mysql

enter image description here

The results are displayed in a table using php

$query = "SELECT * FROM cm_enquiries WHERE enq_event_type = '2' ";
    $select_posts = mysqli_query($con,$query);  
     $counter = 0;
    while($row = mysqli_fetch_assoc($select_posts )) {
      $counter +=1;
        $get_enq_no           = $row['enq_no'];
        $get_enq_event        = $row['enq_event'];
        $get_enq_event_date   = $row['enq_event_date'];
                
        echo "<tr>";   
        echo "<td>$counter</td>";      
        echo "<td><a href='update_wedding_events.php?update=$get_enq_no'><btn class='btn btn-  primary'>view Events</btn></a></td>";
        if(empty($get_enq_event)){
          echo "<td><a href='update_wedding_events.php?update=$get_enq_no' class='btn btn-danger'>Update Events</a></td>";
        }
        else{
          echo "<td>";
          $get_enq_event;        
          $explode_events = explode(",", $get_enq_event);

          foreach($explode_events as $explode){           
            echo $explode . "<br>";
          }
          echo "</td>";
        }//end of else


        echo "<td>$get_enq_amount</td>"; 
        echo "</tr>";
    }
      ?>  

Only relevant Php code is posted above.

The above code outputs in the below table

enter image description here

Till now, there is no issue. However When I try to insert a while loop inside foreach loop, I could not get the desired output

The rest of the rows could not be seen.

foreach($explode_events as $explode){           
            
            $event_name_query = "SELECT * FROM event_types WHERE event_code = '$explode'";
            $select_posts = mysqli_query($con,$event_name_query);

            while($row = mysqli_fetch_assoc($select_posts)) {
            echo  $event_code        = $row['event_code'];                     
              $event_amount        = $row['event_amount'];  
          }   

experiencing issue in table

enter image description here

Been trying this for so many hours, could not get the desired output.. I want all the rows to appear.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • So if I'm following this correctly, there is an outer `while` loop, that has a nested `foreach` loop, and the foreach loop has a nested `while` loop? ... if so, make sure that you're not clobbering the outer-most while loop's `$row` with the same named variable with the while that is inside the foreach. Maybe a `$row2` variable instead? – Paul T. Dec 10 '22 at 03:57
  • @PaulT. Tried with $row2 still not working – Vamshi G Dec 10 '22 at 04:02
  • Then I'm not following the code. Might need to update the example if the code differs from what was originally posted. Maybe view the page source to see if the data is in-between the table elements by searching the output, which won't get rendered if this is the case. – Paul T. Dec 10 '22 at 04:03
  • @PaulT. checked the page source, there is no other data between table elemens – Vamshi G Dec 10 '22 at 05:04
  • 1
    You should never store more than one value in a database column -- storing comma separated list or anything like that is a bad idea, and just leads into all sorts of issues when you try to use that data. To store multiple values, you should have a different table. – James Z Dec 10 '22 at 08:23
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Dec 10 '22 at 12:35

2 Answers2

0

It's not a direct answer to your problem, but may I suggest you do something about your database design? Don't store the selected options as a comma seperated list, store them apart.

At the moment, I guess, you have a table Options like this:

option_id option_name price
1 proposal 10
5 kiss the bride 100
7 receive presents 20
9 throw out drunk uncle 1000
10 divorce 0

And a table Enquiries

enquiry_id options_ids
19 5,7,9

If you want the options selected by a customer, you first have to retrieve the comma seperated list from Enquiries, explode it, select the related options from the Options table, loop them, calculate the price and finally show them.

If you store the Customer selected options like this

enquiry_id option_id
19 5
19 7
19 9

you can with one query get the selected options, the price and their names:

SELECT 
   ENQ.option_id,
   OPT.option_name,
   OPT.price
FROM 
   Enquiries AS ENQ
LEFT JOIN 
   Options AS OPT
ON( ENQ.option_id = OPT.option_id )
WHERE ENQ.enquiry_id = 19

As a result, from one query you get the selected options by the customer, including their names. Only loop the result once for display and calculating the sum:

$result = array(
  0=>[
   'option_id' => 5,
   'option_name' => 'kiss the bride',
   'price' => 100
   ],
  1=>[
   'option_id' => 7,
   'option_name' => 'receive presents',
   'price' => 20
   ],
  2=>[
   'option_id' => 9,
   'option_name' => 'throw out drunk uncle',
   'price' => 1000
   ],
)

And similar if you want to present the customer with the possibility to edit the options after saving, you only have to select all the options in the Options table and join them with the options selected by the cusomer.

SELECT 
   OPT.option_id,
   OPT.option_name,
   OPT.price,
   ENQ.option_id AS is_selected
FROM 
   Options AS OPT
LEFT JOIN 
   Enquiries AS ENQ
ON( ENQ.option_id = OPT.option_id )
WHERE ENQ.enquiry_id = 19

Where the customer has selected the option, you get a is_selected number, otherwhise you get NULL

$result = array(
  0=>[
   'option_id' => 1,
   'option_name' => 'proposal',
   'price'       => 10,
   'is_selected' => NULL
   ],
  1=>[
   'option_id' => 5,
   'option_name' => 'kiss the bride',
   'price'       => 100,
   'is_selected' => 5
   ],
  2=>[
   'option_id' => 7,
   'option_name' => 'receive presents',
   'price'       => 20,
   'is_selected' => 7
   ],
  3=>[
   'option_id' => 9,
   'option_name' => 'throw out drunk uncle',
   'price'       => 1000,
   'is_selected' => 9
   ],
  4=>[
   'option_id' => 10,
   'option_name' => 'divorce',
   'price'       => 0,
   'is_selected' => NULL
   ]
)

It might seems more work for now, but in the end it makes life a lot easier.

Michel
  • 4,076
  • 4
  • 34
  • 52
0

You don't need a while loop inside foreach:

$valueSearch = "";
$valueSearch2 = "";
foreach($explode_events as $explode){    
    $stmt = $conn->prepare("SELECT `event_code`,`event_amount` FROM `event_types` WHERE `event_code` = ?"); 
    $stmt->bind_param("s", $explode);
    $stmt->execute();
    $stmt -> store_result();
    $stmt -> bind_result($valueSearch,$valueSearch2);
    $stmt->fetch();
    $stmt->close();
    echo $explode. "-". $valueSearch. "-". $valueSearch2."<br>";
    }
rauwitt
  • 312
  • 2
  • 4