Forum Moderators: coopster
Try this within the while loop ..
//gets the prefix for the row in question
$pre = substr($row['name'], 0, 2);
//add all the 18 prefixes you'd want displayed here
$pre_arr = array('HP', 'AC');
//if $pre is not empty and is in the array of required prefixes, print or whatever else you want to do..
if (!empty($pre) && in_array($pre_arr, $pre) )
print $row['name'];
- Out of 46 cols, you will always be testing the same 18 cols
- Any time those cols have values other than null or a zero-length string, you want to add a prefix. In other words, you merely want to avoid creating a value (= to the prefix) where currently there is none.
If that's the case, you merely have to test each value. To save me some writing, lets just assume that you have five cols (col1-col5) and want to add prefixes to two (col2, col4) and col1 is your primary key. You do this
$prefix_array ('col2' => 'prefix2', 'col4' => 'prefix4');
$query = "SELECT col1, col2, col4 FROM table1";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$update_query = "";
foreach ($row as $key => $val) // walk through our array of columns
{
if ($val) // only update if it has a value
{
$update_query .= $key . "='" . $prefix_array[$key] . $val . "',";
}
} // close foreach loop
if ($update_query) // if!$update_query, that means all cols were blank. No update
{
$update_query = substr($update_query, 0, -1); // trim off the trailing comma
$update_query = "UPDATE table1 SET " . $update_query; // note space between SET and "
mysql_query($update_query);
} // close update if
} close while loop
This of course does no error checking or anything, but that should pretty much do it for you.
In any case, the script I gave you does most of what you want, but needs two modifications.
1. Instead of getting the data and creating an INSERT, you need to get the data and put it in a table. So instead of $update_query .= SQL statement you just do
$output_row = '<tr>';
....
$output_row .= '<td>' . $prefix_array[$key] . $val . '<td>';
.....
echo output_row . '</tr>';
2. You need to check whether this is a column that needs a prefix. Remember, the ones to which we want to add prefixes are called col2 and col4.
$prefix_cols = array('col2', 'col4');
Now we change our query to
SELECT * FROM table1
Now, inside the foreach statement, first we test that the current field is in our list of cols to be prefixed, then we make sure it has a value, all in one quick if statement:
if (in_array($key, $prefix_cols) && $val)
{
$val = $prefix_array[$key] . $val;
}
Now we put the value in a cell of our table row.
$output_row .= "<td>$val</td>"
I think your answer is among all of the great advice you have gotten here so far but let's see if we can't help you to understand it all a little better.
I think part of the problem here is that we don't quite understand the why of what you are doing. The one drawback of a forum environment is the figuring out of the actual situation often takes longer than if you were sitting right across from each other.
So, let's go back to the problem itself
What is your database structure?
What is it exactly you are trying to do?
do you just want to display data from your table or are you trying to actually do something else with it?
The thing that is difficult to understand is why you need the prefixes just to display data.
Now for these 18 columns in the db if I do not have a prefix they will return like this
125, 352, 12, 452(commas for example purposes only)
No one will be able to figure out which number goes to which attribute.
So this is what I would like it to look like.
AR 125, AC 352, PR 12, Hp 452 etc etc (commas for example purposes only)
I have tried doing this by putting all db output into variables like this.
$AR = "AR" . "$row["AR"]" . " ";
But I still have the bold AR showing whether there is a value or not. So it looks like this.
Item 1 AR 132
Item 2 AR 541
Item 3 AR
Item 4 AR
I only want the prefix to show if there is a value in the db column.
I hope these examples help.
As I said when I started this I am not good at explaining things especially when it comes to something I dont know all that much about.
Thank you to everyone that has and does help. I truly appreciate it.
If you get this working and then want to figure out how to make it more flexible and robust, post back.
Tom
$prefix_array ('col2' => 'prefix2', 'col4' => 'prefix4');
$prefix_cols = array('col2', 'col4');
$query = "SELECT * FROM table1";
$result = mysql_query($query);
$table_rows ="";
while ($row = mysql_fetch_assoc($result))
{
$table_rows .= "<tr>";
$prefixed = "";
foreach ($row as $key => $val)
{
if (in_array($key, $prefix_cols) && $val)
{
$prefixed .= $prefix_array[$key] . $val . ", "; // leaves an extra comma + space which we will snip off later.
}
else
{
$table_rows .= "<td>$val</td>";
}
} // close foreach loop
// the substr() call snips off the last comma and space.
$table_rows .= "<td>". substr($prefixed, 0, -2) ."</td></tr>";
} close while loop
echo "<table>$table_rows</table>";