Skip to content

Instantly share code, notes, and snippets.

@rungiraffe
Created September 26, 2012 22:47
Show Gist options
  • Save rungiraffe/3791135 to your computer and use it in GitHub Desktop.
Save rungiraffe/3791135 to your computer and use it in GitHub Desktop.
For Blog Entry (20120926)
//Functions to calculate congress start and end years
function congressStart($congress) {
$start_calc = $congress * 2 + 1787;
return $start_calc;
}
function congressEnd($congress) {
$end_calc = $congress * 2 + 1789;
return $end_calc;
}
//Creates two-digit start and end years for each Congress
$start = substr(congressStart($congress), -2);
$end = substr(congressEnd($congress), -2);
//Create variables for existing selection or new party entry
$ex_party = mysql_real_escape_string($_POST['party']);
$new_party = mysql_real_escape_string($_POST['insert_party']);
//Check for empty values or values in BOTH text field and dropdown for party and source
if (empty($ex_party) && empty($new_party)) {
die ("You must select an existing or enter a new party in <a href='update.php'>Update Form</a>.");
} elseif ($ex_party=="" && $new_party==""){
die ("You must select EITHER an existing party OR enter a new party in <a href='update.php'>Update Form</a>.");
}
//Create MySQL queries for pulling party_id for an existing party
$party_test = "SELECT party_id from party WHERE party = '$ex_party'";
$party_testq = mysql_query($party_test);
$party_row= mysql_fetch_array($party_testq);
//echo($party_row[0]) or die('There is no existing party record.');
//Create variable containing MySQL Query for inserting a new party
$party_query = "INSERT INTO party (party) VALUES ('$new_party')";
//Check if party exists (party_id > 0)
if ($party_row[0] > 0){
echo ("<strong>Party already exists.</strong>".$break);
echo ("Query: ".$party_test.$break);
$party_id = $party_row[0];
}
// Create new entry if party does not exist
elseif (mysql_query($party_query)){
echo ("<strong>Party update SUCCESS. Added new record:</strong>".$break);
echo ("Query: ".$party_query.$break);
echo ("Data: ".$new_party.$break);
$party_id = mysql_insert_id();
}
// If both fail. Something is wrong.
else {
echo ("<strong>Party update FAILED</strong>".$break.$party_test.$break.$party_query.$break);
}
//Checking that I've captured party_id of existing record or new party_id from created record for join_cong
echo ("Party ID: ".$party_id.$break);
//LATER ON DOWN THE CODE....
$join_congq = "INSERT INTO join_cong (congress_num, bio_id_fk, address_id_fk, party_id_fk)
VALUES ('$congress','$bio_id','$address_id', '$party_id')";
//Update join_cong joiner table and check for failure
if (mysql_query($join_congq)){
echo ("<strong>The join_cong joiner table has been updated</strong>".$break);
echo ("Query: ".$join_congq.$break);
echo ("Data: ".$congress.$comma.$bio_id.$comma.$address_id.$comma.$party_id.$break);
}
else {
echo ("<strong>Joiner join_cong update FAILED</strong>".$break.$join_congq.$break);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment