hey, hey, hey
so i've been working this same script over for a few hours every few days this week and haven't been able to make a single inch's worth of movement forward.
essentially the issue:
i have a db filled with data that has a few fields that need to be split based on a string pattern. if it's easier to do this in straight up SQL then i'm down but would need hlep.
because of the task at hand, i just need to run this "data clean" script once--all the data i'm putting in the db from different files is of an uniform type; it's just not scalable for applications. i put the part of the code that i'm struggling with in a function so it's easier to find. there's something wrong with the ereg() usage but i'm not sure what. i've used similar implementations of ereg with no problems (some in different parts of this same script here).
here's what i have:
<?php
function pattern_check ($pattern_used, $string_used, $setID)
{
echo "---HITS FUNCTION---<br />";
echo "$pattern_used <br />";
echo "$string_used <br /> ";
echo "$setID <br />";
//checks for the pattern, then splits and updates to DB
if (ereg ($pattern_used, $string_used))
{
$parts = ereg ($pattern_used, $string_used, $individual_parts);
$set_name = $individual_parts [0];
$rarity = $individual_parts[1];
echo "set name: $set_name with rarity: $rarity <br />";
}
else
{"echo either it's no string match or you f'd it up. <br />";}
echo "---ends function---<br />";
}//end of function
?>
<?php
if(isset($_POST['go']))
{
include "database.inc.php";
$get_entries = "SELECT * FROM cards";
$get_entries_query = mysql_query ($get_entries);
//start to loop through entries
while ($results = mysql_fetch_array ($get_entries_query, MYSQL_ASSOC))
{
$power_unsplit = $results ['power']; //power from original db upload
$CardID = $results ['cardID']; //cardID associated w/ this entry
$type_unsplit =$results['type']; //type from original db upload
$rarity_unsplit = $results ['rarity']; //rarity from original upload
//checks for coupled power/toughness and then splits the pulled data into power, slash, toughness...as an array 1,2,3
////////////
if (ereg ("^([[:alnum:]]+)\/([[:alnum:]]+)", $power_unsplit))
{
$parts = (ereg("^([[:alnum:]]+)\/([[:alnum:]]+)", $power_unsplit, $individual_parts));
$power= $individual_parts [1];
$power = trim($power);
$toughness = $individual_parts [2];
$toughness=trim ($toughness);
echo "new power: $power and new toughness: $toughness ---- <br />";
//now updates the approriate fields in the db
$update_db = "UPDATE cards SET power ='$power', toughness='$toughness' WHERE cardID='$CardID'";
echo "$update_db<br />";
$update_query = mysql_query ($update_db);
if (!$update_query)
echo mysql_error();
else
{ echo "power and toughness update successful<br />"; }
}//end of pattern check for coupled power/toughness
////////////////
//check to see if there's a - which indicates a couple data pair in the type field, if so split and update
if (ereg ('-', $type_unsplit))
{
$type_parts = split ('-',$type_unsplit);
$type = $type_parts [0];
$type = trim ($type);
$sub_type = $type_parts[1];
$sub_type = trim ($sub_type);
//update db
$update_type = "UPDATE cards SET type='$type', sub_type='$sub_type' WHERE cardID ='$CardID'";
$update_type_query = mysql_query($update_type);
if (!$update_type_query)
echo mysql_error();
else
{ echo "type and subtype update successful<br />"; }
}//end of - check
if (!ereg ('-', $type_unsplit))
{ echo "it's not a creature.<br />"; }
///////////////////
///check to see if there's a space in the rarity and will split
///also string checks against each set as an individual set. (set as an array)
////////////////
////after array of set names and id's, check for pattern in rarity_unpslit
/// if ther'es a match, update db w/ correct mtg_setID and rarity (split)
///////////////////
$set_list = "SELECT * FROM mtg_set_names";
$set_list_query = mysql_query ($set_list);
$array_counter = 0;
while ($set_results = mysql_fetch_array ($set_list_query, MYSQL_ASSOC))
{
$set_names[$array_counter] =$set_results['mtg_set_name'];
$set_dbID [$array_counter] = $set_results['mtg_setID'];
$array_counter++;
}//end of while for set data
$array_counter = 0;
foreach ($set_names as $set_name)
{
$pattern_used = $set_name;
$setID = $set_dbID[$array_counter];
$string_used = $rarity_unsplit;
$pattern_check = pattern_check ($pattern_used, $string_used, $setID);
$array_counter++;
}//end of foreach used to go through set names
}//end of while
//kill mysql connection and end first if
mysql_close();
}//end of $_POST if
?>
</head>
<body>
<?php
if ($admin_message)
{ echo "Admin Message: $admin_message <br />"; }
elseif ($admin_error_message)
{ echo "Admin Message: $admin_error_message <br />"; }
?>
This page cleans up power, toughness and then type, subtype.<br />
<form method="post" action="#" >
Click Go to clean that DB<br />
<input type="submit" name="go" value="go" />
</form>
</body>