Jump to content

PHP explode table rows and columns


karenn1

Recommended Posts

Hey everyone,

 

I hope someone can help me. I have a form with a table like this:

 

<table width="850" border="0" align="left" cellpadding="5" cellspacing="0" id="mytable">
              <tr>
                <td width="5%" align="left"><strong>Date</strong></td>
                <td width="21%" align="left"><input name="admission-date[]" id="admission-date" size="15"/></td>
                <td width="10%" align="left"><strong>Hospital</strong></td>
                <td width="16%" align="left"><input name="admission-hospital[]" id="admission-hospital" type="text" size="15" /></td>
                <td width="8%" align="left"><strong>Doctor </strong></td>
                <td width="17%" align="left"><input name="admission-doctor[]" id="admission-doctor" type="text" size="15" /></td>
                <td width="8%" align="left"><strong>Details</strong></td>
                <td width="15%" align="left"><input name="admission-details[]" id="admission-details" type="text" size="15" /></td>
              </tr>
            </table>

 

And this is the PHP to store the info:

 

//multiple entries for admission history
//admission-date
	$array1 = $_POST["admission-date"];
	$counter1 = 0;
	$string_to_db1 = "";
	$separator1 = "";

	foreach($array1 as $item1){
		if($counter1 <> 0) {
			$separator1 = "|";
		}
  
	$string_to_db1 .= "$separator1$item1";
	++$counter1;
	}
///////
//admission-hospital
	$array2 = $_POST["admission-date"];
	$counter2 = 0;
	$string_to_db2 = "";
	$separator2 = "";

	foreach($array2 as $item2){
		if($counter2 <> 0) {
			$separator2 = "|";
		}
  
	$string_to_db2 .= "$separator2$item2";
	++$counter2;
	}
///////
//admission-doctor
	$array3 = $_POST["admission-doctor"];
	$counter3 = 0;
	$string_to_db3 = "";
	$separator3 = "";

	foreach($array3 as $item3){
		if($counter3 <> 0) {
			$separator3 = "|";
		}
  
	$string_to_db3 .= "$separator3$item3";
	++$counter3;
	}
///////
//admission-details
	$array4 = $_POST["admission-details"];
	$counter4 = 0;
	$string_to_db4 = "";
	$separator4 = "";

	foreach($array4 as $item4){
		if($counter4 <> 0) {
			$separator4 = "|";
		}
  
	$string_to_db4 .= "$separator4$item4";
	++$counter4;
	}
///////

 

I've used jQuery to add a row to the table by clicking on a Plus sign. All of this works perfectly and the data is stored separated with a pipe, ie. 2012-08-12|2012-08-21, etc.

 

The issue I'm having is pulling this back into a page where it can be edited. I'm thinking of using explode but I don't know how to get the table layout as I had it originally as shown above.

 

How do I get it to loop as such and have the label next to the value?

 

 

Thanks in advance,

Karen

Link to comment
Share on other sites

. . . and the data is stored separated with a pipe, ie. 2012-08-12|2012-08-21, etc.

 

Well, there's your problem. Why are you storing data in that manner? You should store each "record" separately. Otherwise you are creating bigger problems for yourself.

 

Then, take the code you have to create the form - and rewrite it to create the form with the previously saved data. Do not copy the code and modify it. Instead you should rewrite that section of code to work with or without data to be used for populating the fields. Then you can include() that block of code when you need to create the form when there is existing data and when there is not.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.