Reading multiple records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jas2803
    New Member
    • Oct 2007
    • 30

    Reading multiple records

    I have used php to import some xml data, I had it set to send the data directly to an sql database. However, when the data is received (the xml source) it is not always correct, names are spelled wrong fields are not filled in, this is beyond my control, I pretty much get what I get. So anyhow, I have set up my php to display all the records to one web page using text fields for each field so I can edit any field and also I have included a check box in front of each record so I can skip adding this record if desired. What I do not know how to do is when I am done correcting the data is traverse through each record to add them to the database.
    Any suggestions?
    Thanks
    JAS
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    well make the checkmark name for each field set to the unique column of that record (some kind of number, ID, or name)

    each record there after should have a [] in the name.

    <input type="text" name="firstName[]" value="" />

    this way PHP will put any fields with firstName[] into an array.

    $_POST['firstName'] will return an array of names if it is set.

    loop through them and you'll have your data.


    -Dan

    Comment

    • jas2803
      New Member
      • Oct 2007
      • 30

      #3
      Originally posted by dlite922
      well make the checkmark name for each field set to the unique column of that record (some kind of number, ID, or name)

      each record there after should have a [] in the name.

      <input type="text" name="firstName[]" value="" />

      this way PHP will put any fields with firstName[] into an array.

      $_POST['firstName'] will return an array of names if it is set.

      loop through them and you'll have your data.


      -Dan
      This is my code for displaying the records, if any data is not correct it is always the first few fields because they are hand entered by the other company, the remaining 39 fields are analysis of the data. Almost every time there are multiple mistakes in the data entry. eventdb is the the analysis data.
      Now what I do not know how to do is after I submit is traverse through the data to have it inserted into the sql db.
      [code=php]
      function displayRecords( $eventdb, $date, $client, $phase, $study) {
      echo '<form action="import_ events3.php" method="post" enctype="multip art/form-data" name="records" target="_self"> ' ;
      $intRecordNumbe r = 0;
      foreach($eventd b as $events) {
      echo '<fieldset>';
      echo '<input name="insert['.$intRecordNum ber.']" type="checkbox" value="" checked>';
      echo '<input name="txtdate['.$intRecordNum ber.']" type="text" value = "'.$date[$intRecordNumbe r].'">';
      echo '<input name="txtclient['.$intRecordNum ber.']" type="text" value = "'.$client[$intRecordNumbe r].'">';
      echo '<input name="txtphase['.$intRecordNum ber.']" type="text" value = "'.$phase[$intRecordNumbe r].'">';
      echo '<input name="txtstudy['.$intRecordNum ber.']" type="text" value = "'.$study[$intRecordNumbe r].'">';
      for($i = 0; $i < 27; $i++) {
      echo "'".$events[$i]["Value"]."', ";
      $eventsarray[$intRecordNumbe r][$i] = $events[$i]["Value"];
      }
      $eventsarray[$intRecordNumbe r][27] = $events[33]["Value"];
      $eventsarray[$intRecordNumbe r][28] = $events[27]["Value"];
      $eventsarray[$intRecordNumbe r][29] = $events[34]["Value"];
      $eventsarray[$intRecordNumbe r][30] = $events[28]["Value"];
      $eventsarray[$intRecordNumbe r][31] = $events[35]["Value"];
      $eventsarray[$intRecordNumbe r][32] = $events[29]["Value"];
      $eventsarray[$intRecordNumbe r][33] = $events[36]["Value"];
      $eventsarray[$intRecordNumbe r][34] = $events[30]["Value"];
      $eventsarray[$intRecordNumbe r][35] = $events[37]["Value"];
      $eventsarray[$intRecordNumbe r][36] = $events[31]["Value"];
      $eventsarray[$intRecordNumbe r][37] = $events[38]["Value"];
      $eventsarray[$intRecordNumbe r][38] = $events[32]["Value"];

      echo "'".$events[33]["Value"]."', ";
      echo "'".$events[27]["Value"]."', "; /* text */
      echo "'".$events[34]["Value"]."', ";
      echo "'".$events[28]["Value"]."', ";
      echo "'".$events[35]["Value"]."', ";
      echo "'".$events[29]["Value"]."', ";
      echo "'".$events[36]["Value"]."', ";
      echo "'".$events[30]["Value"]."', ";
      echo "'".$events[37]["Value"]."', ";
      echo "'".$events[31]["Value"]."', ";
      echo "'".$events[38]["Value"]."', ";
      echo "'".$events[32]["Value"]."'";
      echo'</fieldset>';

      $intRecordNumbe r++;
      }


      echo '<input type="submit" name="UpdateDB" value="Accept"> ';
      echo'</form>';

      }
      [/code]

      -----------------------------------------------------------------------------------------------------
      This was the original query for inserting
      [code=php]
      function buildInserts($e ventdb, $date, $client, $phase, $study) {
      $dbconn = pg_connect("hos t=###.###.###.# ## port=5432 dbname=client user=username password=passwo rd");

      $insert_counter = 0;
      foreach($eventd b as $events) {
      $query = "INSERT INTO events VALUES('$date[$insert_counter]', '$client[$insert_counter]', '$phase[$insert_counter]', '$study[$insert_counter]', ";
      for($i = 0; $i < 27; $i++) {
      $query .= "'".$events[$i]["Value"]."', ";
      }

      $query .= "'".$events[33]["Value"]."', ";
      $query .= "'".$events[27]["Value"]."', ";
      $query .= "'".$events[34]["Value"]."', ";
      $query .= "'".$events[28]["Value"]."', ";
      $query .= "'".$events[35]["Value"]."', ";
      $query .= "'".$events[29]["Value"]."', ";
      $query .= "'".$events[36]["Value"]."', ";
      $query .= "'".$events[30]["Value"]."', ";
      $query .= "'".$events[37]["Value"]."', ";
      $query .= "'".$events[31]["Value"]."', ";
      $query .= "'".$events[38]["Value"]."', ";
      $query .= "'".$events[32]["Value"]."'";

      $query .= ");";
      pg_query($dbcon n, $query);

      $insert_counter ++;
      }

      pg_close($dbcon n);
      }
      [/code]
      Last edited by Atli; Jul 31 '08, 03:26 AM. Reason: Added [code] tags

      Comment

      • jas2803
        New Member
        • Oct 2007
        • 30

        #4
        OK, I have got it working, will have to tidy up a bit but it does what I want it too
        =============== =============== =============== ============
        Code:
        <html>
        	<head>
        		<title>Import XML Events</title>
        	</head>
        	
        	<body>
        
        <?php
        .
        .other functions....
        .
        
        function buildInserts($eventdb) {
        	$dbconn = pg_connect("host=xxx.xxx.xxx.xxx port=5432 dbname=client user=username password=password");
        
        	$insert_counter = 0;
        	echo "size of events". sizeof($eventdb) ."<br>";
        	foreach($eventdb as $events) {
        		$query = "INSERT INTO events VALUES( ";
        		for($i = 0; $i < 42; $i++) {
        			$query .= "'".$events[$i]."', ";	
        		}
        		$query .= "'".$events[42]."');";
        		echo $query."<br/>";
        		 pg_query($dbconn, $query); 
        		$insert_counter++;
        	}
        	echo "<h1>File Inserted</h1>";
        	pg_close($dbconn); 
        }
        
        function displayRecords($eventdb, $date, $client, $phase, $study) {
        echo '<form action="import_events4.php" method="post" enctype="multipart/form-data" name="records" target="_self">' ;
        	$intRecordNumber = 0;
        	foreach($eventdb as $events) {
        		echo '<fieldset>';
        		echo '<input name="insert['.$intRecordNumber.']" type="checkbox" value="'.$intRecordNumber.'" checked>';
        		echo '<input name="txtfield['.$intRecordNumber.'][0]" type="text" value = "'.$date[$intRecordNumber].'">';
        		echo '<input name="txtfield['.$intRecordNumber.'][1]" type="text" value = "'.$client[$intRecordNumber].'">';
        		echo '<input name="txtfield['.$intRecordNumber.'][2]" type="text" value = "'.$phase[$intRecordNumber].'">';
        		echo '<input name="txtfield['.$intRecordNumber.'][3]" type="text" value = "'.$study[$intRecordNumber].'">';
        		for($i = 0; $i < 27; $i++) {	
        			echo '<input name="txtfield['.$intRecordNumber.']['.($i + 4).']" type="text" value = "'.$events[$i]["Value"].'" size="4" maxlength="7">';
        		}
        		echo '<input name="txtfield['.$intRecordNumber.'][31]" type="text" value = "'.$events[33]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][32]" type="text" value = "'.$events[27]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][33]" type="text" value = "'.$events[34]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][34]" type="text" value = "'.$events[28]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][35]" type="text" value = "'.$events[35]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][36]" type="text" value = "'.$events[29]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][37]" type="text" value = "'.$events[36]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][38]" type="text" value = "'.$events[30]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][39]" type="text" value = "'.$events[37]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][40]" type="text" value = "'.$events[31]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][41]" type="text" value = "'.$events[38]["Value"].'" size="5" maxlength="7">';
        		echo '<input name="txtfield['.$intRecordNumber.'][42]" type="text" value = "'.$events[32]["Value"].'" size="5" maxlength="7">';
             	echo'</fieldset>';	
        		$intRecordNumber++;
        	}
         echo '<input type="submit" name="UpdateDB"  value="Accept">';
         echo '<input name="intNumberofRecords" type="hidden" value="'.$intRecordNumber.'">';
        echo'</form>';
        }
        
        if(isset($_POST['UpdateDB'])) {
        	$newdb = array();
        	$datadb = $_POST['txtfield'];
        	$fieldChecked = $_POST['insert'];
        	$CheckedCount = sizeof($fieldChecked);
        	$x = 0;
        	foreach($fieldChecked as $marked) {
        		$fieldChecked[$x] = $marked;
        		$x++;
        	}
        	foreach($datadb as $recordnum){
        		foreach ($recordnum as $datafield) {
        			}
        		}
        	$x=0;
        	echo "Checked Count :  ".$CheckedCount;
        	for($i = 0; $i < $CheckedCount; $i++) {
        		 while($fieldChecked[$i] != $x) {
        			$x++;
        			}  
        		 $n=0;	
        		foreach($datadb[$x] as $fieldsx){
        			$newdb[$i][$n] = $fieldsx;
        			$n++;
        		}
        		$x++;
        	}
        		 buildInserts($newdb);	 	
        }
         displayRecords($db, $date, $client, $phase, $study);
        .
        .
        .
        .
        .
        .
        .
        
        ?>
        	
        	<form action="import_events.php" enctype="multipart/form-data" method="post">
        	XML File:  <input type="file" name="datafile" size="40">
        	<br/>
        	<button type="submit">Submit</button>
        	</form>
        	
        	</body>
        </html>
        Last edited by pbmods; Jan 29 '09, 02:46 AM. Reason: Added CODE tags.

        Comment

        Working...