Tech Musings

Thursday, August 17, 2006

Recordsets, Functions, and Arrays... Oh My!

I just fumbled my way through a very difficult programming stretch over the past 18+ hours or so. I set out to do what I thought would be a fairly simple operation, but the process turned out to be much more demanding and ended up testing my resolve in more ways than one. On a positive note, I learned a bit more about functions and arrays and should be better prepared in future projects now that I understand how these "prickly pears" are implemented. I've used arrays and functions many times before in my projects but in all honesty I never really needed to know exactly how they worked... until NOW!

All I hoped to accomplish was to separate values from a returned query or row from my database into a comma separated list. Seems simple, right? Well...

I discovered early on that I didn't fully understand arrays including how to print or show the actual values found in an array. Shockingly, I was trying stupid stuff like this:

$array = mysql_fetch_array($Recordset1);
echo ($array);

and kept getting the word Array returned. I finally stumbled across the correct way to accomplish this which is to use the print_r($array) function to print and see what the hell is actually IN my array!

Anyway, my over-reaching goal was to pull a record (row) from my MySQL database and print just the NON-NULL field values from record into a comma-separated list which would have the final two elements separated by the word 'and'. I played around with implode, preg, explode, strip, etc. but couldn't seem to get the appropriate combination.

The following threaded discussions and postings helped me "cobble out" my solution:
Array Functions:
More on Array Functions:

The first thing I needed to do was get the results of my recordset into a variable that I could then in turn, turn into an array. To my surprise, neither of these actions worked:

$variable = mysql_fetch_array($Recordset1);
$variable = mysql_result($Recordset1);

I'm not sure what was happening, but the variable contained Resource, id and #5. What the f#@& was this?!?

So, I tried:
$variable = ($row_Recordset1['field_1'] . " " . $row_Recordset1['field_2'] . " " . $row_Recordset1['field_3'] ... etc.

and it worked. Not very elegant (I’m sure there’s a better way) but it was my first hurdle cleared.

This inputted EVERY field into my array, even the fields that were NULL. So the next thing I needed to do was clear out the NULL values. I found this little function which did exactly that:

function cleanArray($array) {
foreach ($array as $index => $value) {
if (empty($value)) unset($array[$index]);
return $array;

But, how to use it? I thought I could just replace $array with $category_array in the function and I’d be good to go. No. That’s not how functions work. In reality, the $array variable is a placeholder for whatever the actual variable is that you’re using against the function. So, you need to call the cleaning function in relative relation to your array and then dump the whole thing into a NEW variable:

$category_clean = cleanArray($category_array);

whew. Okay. The final step was to take this $category_clean variable, count the values left after the cleaning, and then insert the appropriate number of commas AND add the “and” between the last two elements of the list:

$final_category_list = "";
$size = count( $category_clean );
$i = 0;
foreach ( $category_clean as $item ) {
$final_category_list .= $item;
if ( $i < $size - 1) $final_category_list .= ", ";
elseif ( $i == $size - 1) $final_category_list .=" and ";

echo $final_category_list;

Here is the final product used on the details.php page of my photo gallery. The categories listed in the description are a result of all this work! Upon further reflection, I don't think it was really worth all the trouble. ;-)


Post a Comment

Links to this post:

Create a Link

<< Home