I have a database with some information. Using an for each statement im getting all the records from the database.
$row2['test'] is in the database 0 or 1 with 0 being no and 1 being yes. My foreach statement works as it shows the records but i want to change the 0 in the test record into no and 1 into yes. I have tryed with an if statement but shamefully it doesn't seem to work.
foreach( $result as $row2 )
{ echo '<table ><tr><td>' . $row2['name'] . '</td></tr>' . '<tr><td>' . $row2['test'] . '</td></tr>' .'<tr><td>' . $row2['comments'] . '</td></tr> </table><p>'; } ?></p>
Use empty function with the ternary operator to test the value with (check the manual for a full write up of what empty means, 0 is one of the values).
foreach( $result as $row2 ) {
$value .= !empty($row2['test']) ? 'yes' : 'no';
echo '<table ><tr><td>' . $row2['name'] . '</td></tr>' . '<tr><td>' . $value . '</td></tr>' .'<tr><td>' . $row2['comments'] . '</td></tr> </table><p>';
} ?></p>
Use ternary operator, for example:
'<tr><td>' . ($row2['test']? 'yes' : 'no') . '</td></tr>'
Full code:
foreach( $result as $row2 )
{
echo '<table ><tr><td>' . $row2['name'] . '</td></tr>'
. '<tr><td>' . ($row2['test']? 'yes' : 'no') . '</td></tr>'
. '<tr><td>' . $row2['comments']
. '</td></tr> </table><p>';
}
Related
I use an array to get a checkbox list and the code below to get active options flagging respective checkbox (checked):
echo '<tr><td><input type="checkbox" name="user_company[]" value="' . $company_id . '"' . (in_array($company_id, $bar) ? ' checked="checked"' : '') . '"/><label><b>' . $company_nome ."</b>, ". $company_das . " / " . $company_state . "</b><label></td></tr>";
All seems to work correctly, but if I have an Empty Array, I see this Warning:
Warning: in_array() expects parameter 2 to be array, null given in
How can I check if my array is empty before hitting this problem?
Use PHP's empty() function:
if(!empty($arr)) {
echo '<tr><td><input type="checkbox" name="user_company[]" value="' . $company_id . '"' . (in_array($company_id, $bar) ? ' checked="checked"' : '') . '"/><label><b>' . $company_nome ."</b>, ". $company_das . " / " . $company_state . "</b><label></td></tr>";
}
I know this is way out of left field, but i was wanting to see if anyone could help.
I am wanting to add a column, populating the data from a certain variable (the variable i have yet to discover the name of). This is a plugin for wordpress and the developer wont really help.
Last ditch effort i guess. Here is the code:
}$output.='><tr><th>' . __('Request For', 'wpsc-support-tickets') . '</th><th>'
. __('Status', 'wpsc-support-tickets') . '</th><th>'
. __('Last Reply', 'wpsc-support-tickets')
. '</th><th>' . __('Department', 'wpsc-support-tickets') . '</th></tr>';
I added the last column, "Department".
Now, looking at the following code, i cant figure out how to populate that data (assuming i knew the variable even)
$output .= '<tr><td>
<a href="" onclick="loadTicket(' . $result['primkey'] . ',\'' . $canReopen . '\');
return false;" ';
if ($result['resolution'] == strtolower('open') ) {
$resresolution = __('Open', 'wpsc-support-tickets');
} elseif ($result['resolution'] == strtolower('closed') ) {
$resresolution = __('Closed', 'wpsc-support-tickets');
} else {
$resresolution = $result['resolution'];
}
if ($devOptions['disable_inline_styles'] == 'false') {
$output.='style="border:none;text-decoration:none;"';
}$output.='><img';
if ($devOptions['disable_inline_styles'] == 'false') {
$output.=' style="float:left;border:none;margin-right:5px;"';
}$output.=' src="' . plugins_url('/images/page_edit.png', __FILE__) . '"
alt="' . __('View', 'wpsc-support-tickets') . '" /> ' . base64_decode($result['title']) .
'</a></td><td>' . $resresolution . '</td><td>'
. date_i18n( get_option( 'date_format' ),
$result['last_updated']) . ' ' . __('by', 'wpsc-support-tickets') . '
' . $last_staff_reply . '</td>
</tr>';
again - where might i add the variable for the column "department" if the variable were $department_var?
You can add it to the very end of that large chunk of mess before the closing </tr>...
change
...$last_staff_reply . '</td></tr>';
to
...$last_staff_reply . '</td><td>' . $department_var . '</td></tr>';
I have a page with a persisted left nav that is populated based on a database. I need to visually mark the menu entry that corresponds with the content currently presented on the right side of the page. I am having problems getting the check of current page to work. (I am learning PHP and attempting to edit someone else's code, person long gone.)
Here is the code for the menu:
while( ( $row = mysql_fetch_array( $result ) ) && ( $count < $limit ) ) {
$count++;
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" >" . stripslashes( $row['title'] ) . "</a></li>\n";
..etc. Works fine to generate menu list.
Then I think what I want to do is to compare the URI this code produces with the currently loaded page, to determine if I should add a CSS style for current page or not.
So I attempted this:
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == "$this" ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
Got syntax errors. So tried this:
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == $this ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
Still syntax errors. So tried this:
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == $row['id'] ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
Suggestions?
Success at last. I found a way to get this to work, though the code may be less than elegant.
I could not get the comparison of the URI and the $row['id'] to work within the echo statement. So I created a separate function to do the comparison and returned the result to the echo statement as follows:
echo "\t\t\t<li>" . stripslashes( $row['title'] ) . "</li>\n";
function checkRow($myID)
{
if( strstr( $_SERVER['REQUEST_URI'], $myID ) )
{
return " class=\"selected\"";
}
}
You have php tags in your echo statement in your first example
echo "\t\t\t<li><a href=\"" . NEWS_URL . "?show=news&action=show&id=" . $row['id'] . "\" <?php if( $_SERVER['REQUEST_URI'] == "$this" ) echo " class=\"selected\""; ?>>" . stripslashes( $row['title'] ) . "</a></li>\n";
try this
echo "\t\t\t<li><a href=\"".NEWS_URL."?show=news&action=show&id=".$row['id']."\". ($_SERVER['REQUEST_URI'] == $this ?"class=\"selected\"" :"").stripslashes( $row['title'] )."</a></li>\n";
I know this should be simple but I just can't seem to get my head around it.
I have a list of continents in a sql database that I get back using PHP DBO and display in a drop down list. What I then want to do is get the users preferred continent from the sql database and select that one in the list. E.g if the list contains World, Africa, Europe, N America and S America but the users favorite is 'Europe' I want that one selected. $getContinent is the users preference.
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
if ($getContinent != ''){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
I would be most grateful if someone could set me straight as I have found some examples on the internet but have been unable to get them to work :)
Your code should be like this
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
//just check if the option id is equal to the chosen value
if ($getContinent != '' && $getContinent==$row['CONTINENT_ID'] ){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
Its simple, as you guessed :D
You would use something like this:
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] . '">' . $row['CONTINENT_NAME'] . '</option>';
}
I hope that helps!
--al
You can use ternary operator
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] .
($getContinent == $row['CONTINENT_NAME']) ? '" selected="selected"' : '"' . '>' .
$row['CONTINENT_NAME'] . '</option>';
}
With MySQL, I'm able to store large blocks of text in a TEXT column, pull like I would any other column type with no problem.
It seems when I try to do the same with CLOB on Oracle, I get errors.
Here's where I'm pulling:
<?php
$comments = 'SELECT q2_other, q4_comments, q9_describe, q10, q11_comments, q12_describe, additional_comments FROM exit_responses
WHERE sdate BETWEEN \'' . $start . '\'
AND \'' . $end . '\'';
$comments_results = oci_parse($conn, $comments);
oci_execute($comments_results);
while($row = oci_fetch_assoc($comments_results)){
if($row['Q2_OTHER'] != null){
echo '<div class="response">';
echo '<div class="info-bar">';
echo '<h5 class="date">' , date('F j, Y',strtotime($row['SDATE'])) , '</h5>';
echo ($_GET['names'] == 1) ? '<h5 class="name">' . $row['f_name'] . ' ' . $row['l_name'] . ' - ' . $row['title'] . ' - ' . $row['emp_type'] . '</h5>' : '';
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="comments">' , $row['q2_other'] , '</div>';
echo '<div class="clear"></div>';
echo '</div>';
}
}
?>
...and here is what I'm getting when I try to print_r() $row within the while() loop:
[Q2_OTHER] => OCI-Lob Object
(
[descriptor] => Resource id #17
)
...(along with the other columns in the query)
Is there something special I need to be doing with CLOBS or is my syntax just off a bit.
Thanks :)
For LOB columns OCI will return OCI-Lob object, on which you have to call load() or read(int bytes) to get the contents:
$clob_contents = $row['Q2_OTHER']->load();
or you can use OCI_RETURN_LOBS flag like
$row = oci_fetch_array($connection_id, OCI_ASSOC | OCI_RETURN_LOBS) to fetch CLOB like VARCHAR2:
$row['clob_field_name']