How to Get Some div id With If Else in PHP - php

I want to ask about how to change div id in if else condition?
My code
<html>
<div id="div1">
<?php
$page = isset($_GET['page']) ? $_GET['page'] : "";
$page2 = isset($_GET['page2]) ? $_GET['page2] : "";
if ($page == "If1") {
include ".../some.php";
}else if ($page2 == "If2") {
echo '<div id="div2">';
include ".../some2.php";
echo '</div>';
}
?>
</div>
</html>
if im going to page in https://localhost/index.php?page2=If2 i want change <div id="div1> to <div id="div2>
can i get some solution?
sorry for my bad english

Here is one approach for doing that, using the If-else statement before printing the <div ... >
<html>
<?php
$page = isset($_GET['page']) ? $_GET['page'] : "";
$page2 = isset($_GET['page2']) ? $_GET['page2'] : "";
if ($page == "If1") {
echo '<div id="div1">';
}else if ($page2 == "If2") {
echo '<div id="div2">
}
include ".../";
?>
</div>
</html>

Declare variable as html attribute
Declare your variable as a html attribute and assign it via php to your <div>.
<?php
// check if request method is "get"
if ($_SERVER["REQUEST_METHOD"] === "GET") {
if (isset($_GET['page'])) { // if "page"
$divId = 'id="div1"';
} else if (isset($_GET['page2'])) { // if "page2"
$divId = 'id="div2"';
}
}
?>
<html>
<div <?php echo $divId ?>>
<?php include ".../" ?>
</div>
</html>
If you'd like to play around with my code in the browser directly, you can do so here.

Here's the simple answer
<html>
<div id="<?php echo isset($_GET['page2']) && $_GET['page'] == 'IF2' ? 'div2' : 'div1' ?>" >
<!-- rest content -->
</div>
</html>
OR
<html>
<?php $id = isset($_GET['page2']) && $_GET['page'] == 'IF2' ? 'div2' : 'div1' ; ?>
<div id="<?php echo $id; ?>">
<!-- rest content -->
</div>
</html>
Note: You can also use PHP short_open_tag to echo id <?= $id; ?> . If short_open_tag enable in php.ini in server

Related

How to simplify this PHP conditional statement?

I am trying to understand a simple / better way of coding something like this php conditional statement.
<?php if (count($foo_bar) > 1) : ?>
<div class="myDiv1">
Hello!
</div>
<?php endif; ?>
<?php if (count($foo_bar) == 1) : ?>
<div class="myDiv2">
Goodbye!
</div>
<?php endif; ?>
Looking for example, and explanation as to why it may be better. Thanks!
Quite simply in this specific situation you dont need the second if as it can be accomplished with a simple if else
<?php if (count($foo_bar) > 1) : ?>
<div class="myDiv1">
Hello!
</div>
<?php else: ?>
<div class="myDiv2">
Goodbye!
</div>
<?php endif; ?>
<?php
$class = 'myDiv2';
$msg = 'GoodBye!';
if (count($foo_bar) > 1) {
$class = 'myDiv1';
$msg = 'Hello!';
}
?>
<div class="<?php echo $class; ?>">
<?php echo $msg; ?>
</div>
Try using an elseif like below. A bit more compact than the 2 statements.
<?php if (count($foo_bar) == 1) : ?>
<div class="myDiv2">
Goodbye!
</div>
<?php elseif(count($foo_bar) > 1): ?>
<div class="myDiv1">
Hello!
</div>
<?php endif; ?>
First, you should the count function only once.
Second, reduce the duplicated HTML blocks.
<?php
$countFooBar = count($foo_bar);
if ($countFooBar == 1){
$message = 'Goodbye! !';
$cssClass = 'class1';
}elseif( $countFooBar > 1){
$message = 'Hello!'
$cssClass = 'class2';
}
?>
<div class="<?php echo $cssClass ?>">
<?php echo $message; ?>
</div>

ECHO in an ECHO in an IF

Config.php:
<?php
$sidebars = 'TRUE';
$sidebar_left = '<font color="GREEN">Hoejhus9 1#</font>';
$sidebar_right = 'KinJacob - Ejer';
?>
Index.php:
<?php
include 'config.php';
if ($sidebars == "TRUE") {
echo '
<div class="sidebar">
<h1><center>Status</center></h1>
<h3><center>CS:S</center></h3>
'.echo $sidebar_left.'
</div>
<div class="sidebar_2">
<h1><center>Admins</center></h1>
'.echo $sidebar_right.'
</div>';
}
?>
This does not work at all. It is not even displaying the page? Just white!
And if I try to use a print instead of echo it shows "1" at the right place and then the text at a whole diffrent page? What should I do?
One echo statement is all you need:
echo '
<div class="sidebar">
<h1><center>Status</center></h1>
<h3><center>CS:S</center></h3>
'.$sidebar_left.'
</div>
<div class="sidebar_2">
<h1><center>Admins</center></h1>
'.$sidebar_right.'
</div>';
Do not use boolean values as a string. Remove extra echo when concat strings. so:
$sidebars = true;
//^
$sidebar_left = '<font color="GREEN">Hoejhus9 1#</font>';
$sidebar_right = 'KinJacob - Ejer';
include 'config.php';
if ($sidebars == true) {
//^
echo '
<div class="sidebar">
<h1><center>Status</center></h1>
<h3><center>CS:S</center></h3>
' . $sidebar_left . '
</div>
<div class="sidebar_2">
<h1><center>Admins</center></h1>
' . $sidebar_right . '
</div>
';
}
If your web host has PHP shorthand enabled you could do it like this, you can close and reopen the PHP tag and use the shorthand <?= and ?> to echo values
<?php
include 'config.php';
if ($sidebars == TRUE){
?>
<div class="sidebar">
<h1><center>Status</center></h1>
<h3><center>CS:S</center></h3>
<?=$sidebar_left?>
</div>
<div class="sidebar_2">
<h1><center>Admins</center></h1>
<?=$sidebar_right?>
</div>
<?php
}
?>

Show Ads In Middle Of Results

I'm currently using sphider on one of my websites, my questions is how can I break the results page into 2 parts to add a 200px break to place a ad slot.
Code:
<?php
extract($search_results);
?>
<?php if ($search_results['did_you_mean']){?>
<div id="did_you_mean">
<?php echo $sph_messages['DidYouMean'];?>: <?php print $search_results['did_you_mean_b']; ?>?
</div>
<?php }?>
<?php if ($search_results['ignore_words']){?>
<div id="common_report">
<?php while ($thisword=each($ignore_words)) {
$ignored .= " ".$thisword[1];
}
$msg = str_replace ('%ignored_words', $ignored, $sph_messages["ignoredWords"]);
echo $msg; ?>
</div>
<?php }?>
<?php if ($search_results['total_results']==0){?>
<div id ="result_report">
<?php
$msg = str_replace ('%query', $ent_query, $sph_messages["noMatch"]);
echo $msg;
?>
</div>
<?php }?>
<?php if ($total_results != 0 && $from <= $to){?>
<div id ="result_report">
<?php
$result = $sph_messages['Results'];
$result = str_replace ('%from', $from, $result);
$result = str_replace ('%to', $to, $result);
$result = str_replace ('%all', $total_results, $result);
$matchword = $sph_messages["matches"];
if ($total_results== 1) {
$matchword= $sph_messages["match"];
} else {
$matchword= $sph_messages["matches"];
}
$result = str_replace ('%matchword', $matchword, $result);
$result = str_replace ('%secs', $time, $result);
echo $result;
?>
</div>
<?php }?>
<?php if (isset($qry_results)) {
?>
<div id="results">
<!-- results listing -->
<?php foreach ($qry_results as $_key => $_row){
$last_domain = $domain_name;
extract($_row);
if ($show_query_scores == 0) {
$weight = '';
} else {
$weight = "[$weight%]";
}
?>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
<div class="idented">
<?php }?>
<b><?php print $num?>.</b> <?php print $weight?>
<?php print ($title?$title:$sph_messages['Untitled'])?><br/>
<div class="description"><?php print $fulltxt?></div>
<div class="url"><?php print $url2?> - <?php print $page_size?></div>
<?php if ($domain_name==$last_domain && $merge_site_results == 1 && $domain == "") {?>
[ More results from <?php print $domain_name?> ]
</div class="idented">
<?php }?>
<br/>
<?php }?>
</div>
<?php }?>
<!-- links to other result pages-->
<?php if (isset($other_pages)) {
if ($adv==1) {
$adv_qry = "&adv=1";
}
if ($type != "") {
$type_qry = "&type=$type";
}
?>
<div id="other_pages">
<?php print $sph_messages["Result page"]?>:
<?php if ($start >1){?>
<?php print $sph_messages['Previous']?>
<?php }?>
<?php foreach ($other_pages as $page_num) {
if ($page_num !=$start){?>
<?php print $page_num?>
<?php } else {?>
<b><?php print $page_num?></b>
<?php }?>
<?php }?>
<?php if ($next <= $pages){?>
<?php print $sph_messages['Next']?>
<?php }?>
</div>
<?php }?>
<div class="divline">
</div>
I'm also not aware of a live PHP code editor, if you know of one please comment and share so I can add a link!
Presuming $from and $to are the result numbers, so you're displaying "Showing results 10 to 30 of 100" for example:
<div id="results">
<!-- results listing -->
<?php $adbreak = ($to - $from) / 2;
<?php foreach ($qry_results as $_key => $_row){
<?php if ($adbreak == 0) { ?>
<div id="results-adbreak">
<img src="buy-a-car.jpg" alt="one careful owner!" />
</div>
<?php }
$adbreak--;
?>
// rest of your code
This will put a div approximately (give or take one) half way down your page of results. You can obviously replace the ad with a call to whatever you want.
adding something like:
<?php $adbreak = ($to - $from) / 2;
<?php if ($adbreak < 5) $adbreak = -1; ?>
will ensure that it doesn't display at all if the results list is too short.
If you don't know $to and $from in advance, you can still do it, but you'll have to calculate the equivalent from the query result first.

ajax breaking wordpress theme with php javascript

I am attempting to utilize a plugin that uses ajax to help "ajaxify" a wordpress theme. The plugin works great on almost all the pages.
The way it works is that it replaces the content from within a container div that you specify. In my instance it was "#newwrap". That does it's job, the only issue is that on reload the javascript/jquery functions from within the content has to be called again.
Within the plugin there is a place where you can indicate what to reload. I have gotten most of the other parts to work correctly, this one is the one that is stumping me though.
I have a page in the theme that utilizes filters with php. When I click on that specific page the page looks like it renders in css etc except nothing shows and the filters do not work. I look at the html behind it with firebug and the content is there just not being displayed.
Here is the link to the site: http://nex.vyralmedia.com
Once there please click on portfolio > image portfolios
If you click on that page it will load and just show the filters, but they wont work and the content will not display. If you refresh that page you will see what it is supposed to look like without using ajax.
Here is the link to the wordpress plugin: http://wordpress.org/extend/plugins/advanced-ajax-page-loader/
So in this case I am looking for the reload code I should use, or just some way to get that portion working properly.
Any Help would be greatly appreciated.
Here is the template page code for it:
<?php
/*
The template that is used to render pages that are targeted by the multiple portfolio behavior of Prime.
*/
get_header(); ?>
<div id="newwrap">
<?php roots_content_before(); ?>
<?php roots_main_before(); ?>
<?php
global $prime_portfolio;
$portfolio_instance = get_option(PRIME_OPTIONS_KEY);
$show_filter = false;
$page = get_queried_object();
foreach ($portfolio_instance['portfolio_instance_slider'] as $p) {
if(key_exists('portfolio_show_filters', $p) && $p['portfolio_show_filters'][0] == 'Yes' && $p['portfolio_page'] == $page->ID) {
$show_filter = true;
}
}
?>
<div class="main portfolio-main <?php if($show_filter) { echo 'show-filter'; } else { echo 'no-filter'; }?>" role="main">
<div class="subheader-wrapper">
<div class="container_12">
<div class="grid_12">
<div id="subheader">
<?php
global $post;
global $prime_frontend;
$prime_frontend->prime_title_and_subtitle();
?>
<?php if($show_filter) { ?>
<div class="table select-table">
<select class="filter">
<option data-filter="*"><?php echo get_portfolio_all_filter_text(); ?></option>
<?php
global $prime_portfolio;
$prime_portfolio->render_all_filter_list_item();
$page = get_queried_object();
$portfolio_instance = get_option(PRIME_OPTIONS_KEY);
$filters = NULL;
foreach ($portfolio_instance['portfolio_instance_slider'] as $p) {
if ($p['portfolio_page'] == $page->ID) {
$filters = isset($p['portfolio_filters']) ? $p['portfolio_filters'] : NULL;
break;
}
}
if (!empty($filters)) {
foreach ($filters as $fil) {
$f = get_term($fil, 'portfolio_filter');
?>
<option data-filter='article[data-filters*="<?php echo $f->slug; ?>"]'>
<?php echo $f->name; ?>
</option>
<?php
}
}
?>
</select>
</div>
<?php } ?>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="content-wrapper">
<div class="overlay-divider"></div>
<?php if($show_filter) { ?>
<div class="filter-wrapper">
<div class="table">
<ul id="filters">
<?php
$prime_portfolio->render_all_filter_list_item();
$page = get_queried_object();
$portfolio_instance = get_option(PRIME_OPTIONS_KEY);
$filters = NULL;
foreach ($portfolio_instance['portfolio_instance_slider'] as $p) {
if ($p['portfolio_page'] == $page->ID) {
$filters = isset($p['portfolio_filters']) ? $p['portfolio_filters'] : NULL;
break;
}
}
if (!empty($filters)) {
foreach ($filters as $fil) {
$f = get_term($fil, 'portfolio_filter');
$prime_portfolio->render_filter_list_item($f);
}
}
?>
</ul>
</div>
<div class="overlay-divider bottom"></div>
<div class="clear"></div>
</div>
<?php } ?>
<?php
$page = get_queried_object();
$page_portfolio_properties = $prime_portfolio->get_portfolio_options($page->ID);
global $wp_query;
$temp_query = $wp_query;
$orig_query_vars = $temp_query->query_vars;
$args = $prime_portfolio->get_portfolio_item_args_for($page->ID);
$posts_per_page = -1;
if (isset($page_portfolio_properties['portfolio_posts_per_page'])) {
$posts_per_page = $page_portfolio_properties['portfolio_posts_per_page'];
$posts_per_page = empty($posts_per_page) ? -1 : intval($posts_per_page);
}
$args['posts_per_page'] = $posts_per_page;
if (!empty($orig_query_vars['paged'])) {
$args['paged'] = intval($orig_query_vars['paged']);
}
else if (!empty($orig_query_vars['page'])) {
$args['paged'] = intval($orig_query_vars['page']);
}
$wp_query = new WP_Query($args);
$paginated = $wp_query->max_num_pages > 1 ? 'paginated' : '';
?>
<div class="portfolio-wrapper">
<div class="row-fluid clearfix page-container">
<div class="span12">
<!--PAGE CONTENT-->
<div class="prime-page prime-full-width prime-portfolio <?php echo $paginated; ?>">
<div id="masonry-container">
<?php get_template_part('loop', 'portfolio'); ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
</div>
<?php roots_main_after(); ?>
<?php roots_content_after(); ?>
</div>
opacity for your article items is set to 0 in style.css line 4293 :
article.item {
opacity: 0;
}
You should remove that rule, or modify your js code to correctly set opacity after ajax loading.

How can I better this code?

I have this code :
<div class="boxContentScroll">
<?
while($row=mysql_fetch_array($query, MYSQL_NUM)) {
?>
<div class="boxAddCategory<? if($row[1]==1) echo "Yes"; else echo "No"; ?>">
<div class="boxAddCategory1">
<? echo $row[0]."<br />"; ?>
</div>
<div class="boxAddCategory2">
<?
if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1)) {
?>
<input type="checkbox" <? if($row[1]==1) echo "checked='checked'" ?> value="categories[]" />
<?
} else echo " "
?>
</div>
</div>
<?
}
?>
</div>
but is not so good read it (just watch the number of ?> or <?). What can you suggest to improve it? Thanks
If you want this code to run on all environments, don't use short_tags (<?). But if you're running this on your own server, you can disregard it.
Use PHP's alternative syntax for control structures. This will make it much more readable.
Don't mix your business logic with your view logic. Either setup your own MVC stack or use a templating engine if you want.
Well I would start with replacing if with short version:
<?php echo ($row[1]==1) ? "Yes" : "No"; ?>
You could write this:
<? if($row[1]==1) echo "Yes"; else echo "No"; ?>
like this:
<?= $row[1] == 1 ? 'Yes' : 'No' ?>
And you could replace this:
if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1)) {
with this:
if ( $admin ) {
and put this before the while loop:
$admin = isset($_SESSION['admin']) && $_SESSION['admin'] == 1;
Any time you're in a loop and have a static condition to check like this:
if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1))
Define it in a variable it outside the loop. If it won't change within the loop, you don't have to check each time, only once. This becomes relevant when doing resource intensive checks and function calls that won't produce a different result outside the loop.
<div class="boxContentScroll">
<?php while($row=mysql_fetch_array($query, MYSQL_NUM)) : ?>
<div class="boxAddCategory <?=($row[1] == 1 ? 'Yes' : 'No')?>">
<div class="boxAddCategory1">
<?=$row[0].'<br />'?>
</div>
<div class="boxAddCategory2">
<?php if((isset($_SESSION['admin'])) && ($_SESSION['admin']==1)) : ?>
<input type="checkbox" <?=($row[1]==1 ? 'checked="checked"' : '')?> value="categories[]" />
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
</div>
Using a HEREDOC:
<div class="boxContentScroll">
<?php
while ($row=mysql_fetch_array($query, MYSQL_NUM)) {
$class = ($row[1]==1) ? 'Yes' : 'No';
$checked = ($class) ? 'checked="checked"' : '';
$data = $row[0];
$admin = (isset($_SESSION['admin']) && $_SESSION['admin']==1) ? 1 : 0;
echo <<< HTML
<div class="boxAddCategory{$class}">
<div class="boxAddCategory1">
{$data}
</div>
HTML;
if ($admin) {
echo <<< HTML
<div class="boxAddCategory2">
<input type="checkbox" value="categories[]" {$checked} />
</div>
HTML;
}
echo <<< HTML
</div>
HTML;
}
?>
</div>

Categories