Forum Moderators: coopster
I have a table where there is column name Active and the answer is Yes or No.
Now I want to change the format of the text in where the answer to column is No (as being member inactive)
this is initial query being written
global $conn;
$strSQLExists = "select * from primary_records where primary_records.active='No'";
$rsExists = db_query($strSQLExists,$conn);
$data=db_fetch_array($rsExists);
if($data)
{
echo("no");
// if record exists do something
}
else
{
// if dont exist do something else
}
the problem is the echo just display "no" on top of page.
1) I want to display the font in red or whatever where the memeber is inactive("No") so that is is obvious when looking at list.
Is this something not possible (i guess not!)
2) I don't know how to call the result of mysql record in php and actually make manuplation on that record from php?
I guess we need to use $dal or something!(correct me if I'm wrong)
First, I don't see a db_query function (?) Is this an internal program object or from a library? For the sake of simplicity, I will cite mysql_query() instead.
What if, for whatever reason, you want to change the value of "Active" from No" and "Yes" to "Active" or "Inactive"? You'd have to modify the records. Even an enum() would require modification of the database.
Second, when you search a database, integer fields will always be faster, which becomes far more apparent when the database increases in size.
So first stop is to make Active a boolean, tinyint(), or int() field. So now when you select you get 1 or 0.
$statuses = array ('No','Yes');
but any time you want to change it,
$statuses = array ('Inactive','Active');
I prefer using a number over a boolean because the possibility may exist you want to add another status, and you can do this with a number.
$statuses = array ('Inactive','Active','Opted Out','Fell off the planet');
(0,1,2,3, it becomes relevant below)
As mentioned, an example using mysql_query and mysql_fetch_array. A while loop is required to parse through $data, as this will be a row set.
$strSQLExists = "select * from primary_records where primary_records.active=0";
$rsExists = mysql_query($strSQLExists);
while ($data=mysql_fetch_array($rsExists)) {
echo "Inactive data was found for record " . $data['id'] . " and it is " . $statuses[$data['Active']];
// the above should echo "Yes, No" or "Active, Inactive" based on which array you choose
}
if (! $data) {
echo " no records found";
}
To answer this,
1) I want to display the font in red or whatever where the memeber is inactive("No") so that is is obvious when looking at list.
2) I don't know how to call the result of mysql record in php and actually make manuplation on that record from php?
I would not use a where statement (unless it's a search only for inactive records.) Instead, I'd go through all records, like so.
(CSS file)
.warning { color: #ff0000; font-weight:700; }
.ok { color: #00a452; font-weight:700; }
Then in PHP,
$red_style="warning";
$green_style="ok";
$data=$results=NULL;
$strSQLExists = "select * from primary_records";
$rsExists = mysql_query($strSQLExists);
while ($data=mysql_fetch_array($rsExists)) {
// This acts as a switch based on the value of Active
$thisStyle=($data['Active']==0)?$red_style:$green_style;
$results .= '<tr><td>' . $data['id'] . '</td>';
$results .= '<td>' . $data['first_name'] . '</td>';
$results .= '<td>' . $data['last_name'] . '</td>';
$results .= '<td class=' . $thisStyle . '">' . $statuses[$data['Active']] . '</td></tr>';
}