Monday, July 30, 2018

Assigning DIVs to php variables and looping on multiple hidden inputs

Leave a Comment

I have a CMS that I've built allowing users to create a 'Page' made up of different panels each with their own content. I've made this work so that a user can create a page with one panel and one textarea of content but I still can't figure out how to do this for multiple panels/content.

Currently, if the page load gets the value of '1' from the URL value, it loads html templates with a fullwidth div and halfwidth div. I'm trying to set the panel type of each with hidden input types and they each have their own tinymce text area.

<?php if($value == 1){?>     <form method="post">     <div class="col-lg-12 fullWidth" id="full">         <input type="hidden" name="fullWidth" value="">         <div class="fullContent" style="background-color: white; height: 100%;">             <form  id="form-data3" method="post">               <textarea class="original" id="mytextarea3" name="fullText">Some Text Here</textarea>               <input type="submit" value="Save Content">             </form>         </div>     </div>      <div class="col-lg-12 halfWidth" id="half">         <input type="hidden" name="halfWidth" value="">         <div class="halfContent" style="background-color: white; height: 100%;">             <form  id="form-data4" method="post">               <textarea class="original" id="mytextarea4" name="halfText">Some Text There</textarea>               <input type="submit" value="Save Content">             </form>         </div>     </div>     </form>  <?php } ?> 

Once this is done and they go to save page, there is another form that lets them set the title of the page and it also gets the page type ($value from above)

<form action="addPage.php" method="post"> <input type="hidden" name="pageType" value="<?php echo $value;?>">//This comes from the url value <input class="form-control" id="addTitle" name="addTitle"> <input type="submit" name="Save Page"> 

The problem is, when I now call my addPage.php script to insert the records, I don't know how to pass the values correctly so that I add one page record (the $value for page_type_id and the Title) but then insert 2 content and panel records for the text areas.

Here's my expected insert in the case of the above code:

pages  ID | Title    | page_type_id 1  | TitleNew | 1     /*this comes from $value*/  content  ID | Content 1  | Some Text Here 2  | Some Text There  panels  ID | panel_type_ID | page_id | content_id 1  |     1         |     1   |   1 2  |     2         |     1   |   2 

This works for one insert in all 3 tables but if I can set multiple panel types to each div, how can I modify this to still insert the one page record but successfully account for multiple panel and content?

Here's the add page script

//Insert Page $title = $_POST['addTitle']; $page_type = $_POST['pageType'];   $addpage = " INSERT INTO pages (title, page_type_id) VALUES ('$title','$page_type'); "; $mysqlConn->query($addpage)  $page_id = $mysqlConn->insert_id;  //Insert Content $content = $_POST['page_content'];  $addContent = " INSERT INTO content(content) VALUES('$content'); ";  $mysqlConn->query($addContent);  $cont_id = $mysqlConn->insert_id;  //Insert panel(s) $panelID = $_POST['panelType']; $addPanel = " INSERT INTO panels(panel_type_id, page_id, cont_id) VALUES ('$panelID', '$page_id', '$cont_id'); "; $mysqlConn->query($addPanel); 

2 Answers

Answers 1

I guess your problem is handling multiple data collection. use [] in your element names to collect more than one data of the same collection.

<textarea class="original" id="mytextarea3" name="fullText[]">Some Text Here</textarea> 

and while saving you get an array from fullText . Loop through that and save it.

if you have multiple documents in the same form. use the key in the first loop to access the respective data from the other elements array.

Hope it helps!!

Answers 2

You're manage it wrong, it should be:

pages  ID | Title    | page_type_id 1  | TitleNew | 1     /*this comes from $value*/  content  ID | Content 1  | Some Text Here 2  | Some Text There  panels  ID | panel_type_ID | page_id | content_id 1  |     1,2         |     1   |   1,2 

I can't find panelType in your form but, You should detect the panels Ids which user has chosen and then send it to php, Example:

if user choose the panel (1) and the panel (2) your script should wrote it here:

<input type="hidden" value="1,2"> 

We separate the panels Ids with a comma because we will need it when we get the data from the database.

How to get the panels IDs:

After we get the data from database it will be like 1,2 and this won't work for you so how to get them in array?

It's so simple just do this:

<?php      $string = "1,2";     $get = explode(',', $string ); // explode with ,  ?> 

Now, You have your array which will look like this:

Array ( [0] => 1 [1] => 2 ) 

how to use it?

You can use a loop:

<?php      $string = "1,2";     $get = explode(',', $string ); // explode with and      $count = count($get);      for ($i=0; $i < $count; $i++) {          $PaN = $get[$i];         /*Some code....*/     }  ?> 

I hope i understand you.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment