Database driven PHP menu with check for current page - php

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";

Related

PHP -displaying 12 items of an associative array with specific key and value

<?php
foreach($catalog as $id=> $item) {
echo get_item_html($id,$item);
}
// here is the functions page code :
function get_item_html($id,$item){
$output = "<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p>View Details</p>"
. "</a></li>";
return $output;
}
?>
I have used it for displaying product categories i checked several time but I did find any error but it display undefined variable
please help me to find out this solution

php echo info database using foreach change 0 to no

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>';
}

How would I add another column and populate it?

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>';

Call specific variable based on what another variable is set to.(PHP)

I am not exactly sure how to word this in the form of a short question...the above called up many different answers that were not related, but I apologize if this has been asked before.
I am trying to make a variable name itself based the value of another.
I could simply create if statements for each one, but if I were able to declare the variable the way I want, it would save me about 20-30 lines of code, and make future additions much easier.
Here is a better description.
This is the code that I am using at the moment. It is within a shortcode function for wordpress, to create a button based on user-given parameters.
extract(shortcode_atts(array(
'size' => 'medium',
'link' => '#',
'text' => ''
), $atts));
$large_button_img = of_get_option('large_button_arrow_upload');
$button_pos = of_get_option('button_image_position');
if($button_pos == 'right' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'>" . $text . "<img src='" . $large_button_img . "' id='button_img' alt='button image' /></a>";
}elseif($button_pos == 'left' && !empty($button_img)){
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'><img src='" . $large_button_img . "' id='button_img' alt='button image' />" . $text . "</a>";
}else
{
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
In the above:
The function pulls the value of "size", "link" and "text" from the user given shortcode and generates a button. It sets the class based on the size...
At the moment I have it to where the user can set different images for a large, medium and small button.
Question:
IS IT POSSIBLE
to return the name of the image source based on what size is set to.
soooo basically the equivalent of?
img src = '" . $($size)_button_img . "'
Where it places value of $size in the name of the variable that it pulls to tell it which image source to pull afterwards? (so the proper equivalent of the above would produce something like
img src= '" . $large_button_img ."'
or, if the user has medium selected
img src= '" . $medium_button_img . "'
If possible this save myself having to write if-statements for every possible option (basically copy the set of if-ifelse-else from above, everytime I have a new size setting available)...which eventually may become more of an efficiency issue.
Thanks in advance for any help that can be given :)
ALSO
Please ignore any syntax errors in the above set of code...I am working on this as you read this, so more than likely, if you see something wrong, it has already been fixed.
You can simple use function for your logic:
function somethingWithImage($img, $pos) {
if ($pos == 'right' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'>" . $text . "<img src='" . $img . "' id='button_img' alt='button image' /></a>";
} elseif ($pos == 'left' && !empty($button_img)) {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'><img src='" . $img . "' id='button_img' alt='button image' />" . $text . "</a>";
} else {
$the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>";
}
return $the_button;
}
And just call it whenever you want with any arguments.
What you are looking for, are variable variable names.
Short example:
$hello_text = 'Hello World!';
$bye_text = 'Goodbye World!';
$varname = 'hello';
echo ${$varname}_text; // Hello World!
// Is the same as:
echo $hello_text; // Hello World!
As I mentioned, you can use interpolation to create variable variables:
$src = ${"{$size}_button_img"};
But may I suggest using arrays instead? You get cleaner and understandable code:
$sizes = array(
'large' => ...,
'medium' => ...,
);
if(isset($sizes[$size]))
$src = $sizes[$size];

Want to get contents inside a div via php

Okay, so I been trying to get specific content from different div classes to put in a table. I'm am customizing my wordpress site. Here is the code I'm having trouble with
//loops through posts
while ( have_posts() ) : the_post();
$doc = new DomDocument;
$doc ->validateOnParse = true;
$link = " ' " . get_permalink() . " ' ";
//this is where the trouble lies. get error
/*Warning: file_get_contents( ' http://www.uwarc.org/?p=60 ' ) [function.file-get-contents]: failed to open stream: No such file or directory in /home/uwarco5/public_html/wp-content/themes/thesis_185/custom/custom_post_loop.php on line 98
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Empty string supplied as input in /home/uwarco5/public_html/wp-content/themes/thesis_185/custom/custom_post_loop.php on line 98 */
$doc->loadHtml(file_get_contents( $link ));
echo "<tr onclick=" . $quote . "DoNav(". $quote1 . "get_post_class('site', null) " . $quote1. ");" . $quote . ">" ;
echo "<td align =" . $quote . "center" . $quote . " >" ;
echo "<div id =". $quote . "org_logo" . $quote . "> <img src =". $quote ;
var_dump($doc->getElementByClass('img')) ;
echo $quote . "width = ".$quote. '150' . $quote. "; </div> " ;
the_title();
echo "</td>";
echo "<td>" . var_dump($doc->getElementByClass('courses')) . "</td>";
echo "<td>";
var_dump($doc->getElementByClass('description'));
echo "</td>";
echo "</tr>
endwhile;?>
Load the post content:
$doc->loadHTML( apply_filters( 'the_content', get_the_content() ) );

Categories