How to determine whether something is in a conditional statement in PHP? - php

I have a copy of a PHP website and there are a lot of pages and a lot of code there. It uses conditional statements to determine what contents should be printed very much. Here's an example(there may be a lot of snippets like the one below):
<?php if($countryA): ?>
<div myattr="myvalue">
<?php elseif($countryB): ?>
<div myattr="myvalue">
<?php elseif($countryC): ?>
<div myattr="myvalue">
<?php elseif($countryD): ?>
<div myattr="myvalue">
<?php endif; ?>
Let's say, I don't care what it is in countryA,countryB and countryD, I only care if there is <div myattr="myvalue"> in countryC.
If I want know how many times <div myattr="myvalue">" will occur if $countryD is true, I need to perform a search among all the files in my website directory using keyword <div myattr="myvalue"> and check if there is <?php elseif($countryC): ?> in the context for every search result, which is really a huge burden...
Are there any ways to help me solve this problem efficiently? Do I need a PHP parser if I want to do this programmatically?

Either you need a parser or in other words , you have to save the format of your code or controls flow in a file or database and apply logic according to that saved data or flow.
I hope you get the sense.

Related

PHP - using include, can i execute on a different div in the page?

I have a small problem, and I'll try to break it down into a smaller one so I can explain it properly.
I'm working on a web application and I have a couple of divs, such as this:
<div class="1">
//search bar
</div>
<div class="2">
include_once 'actioncontroller.php';
</div>
<div class="3">
</div>
In the actioncontroller.php I'm having an action controller which decides what action to take depending on what's pressed on the page. I've put it in the second div because ultimately that's where I want to print everything.
My question is, is there any way that I can use the code from the second div in the first one, without it printing it there? Basically I want the search bar from div one to do/print the same thing as the one in div 2 does, but I know(think) that PHP can't see code above the include_once, and if I include the actioncontroller.php in the first div it will print it there, instead of printing it in the second one, as I want.
Hope I was clear enough, it's not a problem of coding, it's just a matter of how can I read the script in the first div and then run it in the second one...
Thanks in advance
My question is, is there any way that I can use the code from the second div in the first one, without it printing it there?
Yes, but the best solution is to change the code you've already written. In the long-term, it is vitally important that you minimize your "procedural" PHP code, so that nothing ever happens simply by include/require-ing a file.
Trust me on this, it works for toy project, but it always leads to insanity and pain in the end. For example, don't put this in a file:
<?php
echo("Header section");
This is bad because you have no choice about when it prints. This is a step up:
<?php
function WriteHeader(){
echo("Header section");
}
Even better would be to use classes an autoloading, but that's probably more than you need to hear right now. With that kind of approach, your main page would look more like:
<?php
// This next line simply makes the class ActionController *available*,
// it does NOT cause new things to happen on its own
include_once("actioncontroller.php");
?>
<div class="1">
<?= ActionController::MakeSomeHTML(); ?>
</div>
<div class="2">
<?= ActionController::MakeSomeHTML(); ?>
</div>
<div class="3">
</div>
This code takes the output of actioncontroller.php and saves it into a variable, which can be echo'd multiple times.
<?php
ob_start();
include_once 'actioncontroller.php';
$output = ob_get_contents();
ob_end_clean();
?>
<div class="1">
<?php echo $output; ?>
</div>
<div class="2">
<?php echo $output; ?>
</div>

Can a Joomla module “know” what position it's in? (2)

This is my very first question at StackOverflow.
I was trying to post comment in the post Can a Joomla module "know" what position it's in?, but could find no way to do that so I have to post another question here. Any tips regarding how to do this properly is greatly apprieciated.
Anyway, here is my questions:
I tried the code mentioned in the above post but it seemed didn't work. I'm not only quite new to PHP, but also coding, so I'm not sure if it's me or the code. here's what I did to the module's default.php file:
1)to ensure I insert the code at the right place, I insert
<?php echo(print_r($module)); ?>
and it output 1 at the right position;
2)the position-name that I need to determine is "showcase-a", so I insert code
<?php if ($module->position == 'showcase-a'):?>test<?php endif;?>
to the above place, but this time it does't show anything;
3)then I tried this code:
<?php if ($module):?><span>test</span><?php endif; ?>
But still it does't display "test" at the position as I expected.
4)I tried
<?php if (1):?><span>test</span><?php endif; ?>
and the test "test" displays as good. So I didn't code the IF statement wrong.
Now I'm totally lost. Since print_r($module) outputs 1, $module must be positive, why PHP ignore test in 3)? This is just a side question for the sake of PHP learning. What I still need to solve is let the module determine which position itself is in.
Please help. Thank you!
I'm not sure about using it in the template (although it doesn't seem wrong altogether) but my modules oftentimes access the position like this:
$module->position
in the module (so mod_something.php) so try to put it there, if it's available just set a variable and it will be available in the view too.
If $module->position didn't work for you. $module->position may only work for joomla 1.7, 2.5 and + but i'm not sure. $module->position works in joomla 2.5.
Otherwise you need to make a module.php function
Check out the file
/templates/you-template/html/modules.php
For example you make your position the following in your template (for example index.php)
<jdoc:include type="modules" name="header" style="includeposition" />
inside of
/templates/you-template/html/modules.php
You make a function like this :
<?php
function modChrome_includeposition($module, &$params, &$attribs){
//Set a value to $module->position
$module->position='WHATEVER YOUWANT';
?>
<div class="moduletable <?php echo $params->get('moduleclass_sfx').' '.$module->name; ?>">
<?php
if($module->showtitle){
?>
<div class="modtitle">
<div class="fleche">
<span>
<?php
echo $module->title;
?>
</span>
</div>
</div>
<?php
}
?>
<div class="modcontent">
<?php echo $module->content; ?>
<div class="clear">
</div>
</div>
</div>
<?php
}
?>
make different functions names that sets different values if needed.
the function name will match the syle you set:
modChrome_includeposition($module, &$params, &$attribs)
because
style="includeposition"
inside of your jdoc.

New(relatively) to code igniter : some basic questions

I'm typing this on an iPad so forgive me if being concise is a bit rude. My question is:
Is it ever ok to have simple logic inside a view? For instance,
<HTML>
<!-- ... Stuff
-->
<?php if($this->session->userdata('authorized'): ?>
<p>You are authorized</p>
<?php else: ?>
<p>You are not authorized</p>
<?php endif; ?> // Question 1, is this the proper use of an endif:?
<!-- .. Stuff
->>
</HTML>
/* Starting to type the rest of message on my laptop. Big thank yous to the coders on
this site who made my pc login transfer my unsaved, half typed iPad post */
The above was just a leftover comment in the code that would have made the limited php use look ugly. I wanted to let it be seen though.
Anyways, on to question #2:
Is it even proper to use a simple conditional like this in a view?
Thanks for reading, and hello again.
In answer to the first question:
That is the proper use of an endif, all is valid and recommended way by codeigniter.
In terms of the second question, this method can be used in a view file; however I would recommend using it in the $data array passed to the page meaning it will be accessed as $authorised; I say this as it will make more sense to a front end designer.
Information on the $data array can be found here, just navigate to "Adding logic to the controller".
I hope this is of help to you.
I recommend you to use differents views in the controller for each case:
// In the controller
if($this->session->userdata('authorized')
$this->load->view('not_autorized.php');
else
$this->load->view('view.php');
So you get clean code an views.
Use the Language class to store your 'You are authorized' and 'You are not authorized' text. Do your session check in the controller, and pass the correct language value to the view in the data array.
Edit: Additional question from STONYFTW:
What approach should one take with a bit of more complex code, such as:?
<?php if(!$this->session->userdata('isLoggedIn')): ?>
<div id="login_form">
<?php echo form_open('login/validateCredentials'); ?>
<?php echo form_input('username', 'Username'); ?>
<?php echo form_password('password', 'Password'); ?>
<?php echo form_submit('submit', 'Log In'); ?>
<div id="login_form_link_container">
<?php echo anchor('login/register', 'Register')." ".anchor('login/recover','Forgot Pass?'); ?>
</div>
</div>
<?php endif; ?>

How do I make a div invisible unless "?added=1" is contained in the page URL?

I'm trying to figure out how to make a div invisible when someone goes to www.mysite.com/examplepage.php, but is visible when someone goes to www.mysite.com/example.php?added=1. Could someone be wonderful enough to explain this to me? The purpose of the div is give a message to users directed to the ?added=1 page - oddly enough something this useful is surprisingly hard to find instructions for. I'm a beginner, so if it's a simple thing to do I'd be really appreciative if someone gave me an example piece of code that would do this.
Thanks a lot
In your HTML/PHP template...
<?php if (isset($_GET['added']) AND $_GET['added'] == '1'): ?>
<div>
...
</div>
<?php endif; ?>
Though it would be better to process the flag in some controller code and then set a flag, so you don't have to use the isset() and the $_GET directly in your view.
<?php if (isset($_GET['added']) && $_GET['added'] == 1): ?>
<div>thediv!</div>
<?php endif ?>
Should get your started. The $_GET['added'] == 1 is optional in your case though.
<?php if (#$_GET['added'] == 1): ?>
<div>thediv!</div>
<?php endif ?>
Can also be used as a shorter version (although I would recommend just using only isset($_GET['added']) in your case, since I doubt you care about the =1 at the end).
The above answers are correct, but if you need to hide it but leave it in the source, something like this:
<div <?php if ($_GET['added']==1)echo "style='display:none'"; ?>>
hidden from the page, visible in view source.
</div>
there are several reasons one want to leave it in the source, such as showing the div when certain button clicked.

Can php divide the page into small block and gradually appear?

Can php divide the page into small block and gradually appear?
I divided my main page into several small blocks, like:
<?php
include('header.php');
<div id="content">
<!-- some content -->
</div>
include('partone.php');
include('parttwo.php');
include('partthree.php');
include('footer.php');
?>
I need open the page, first load header.php, div#content, footer.php, then gradually loaded partone.php, parttwo.php, partthree.php. Is there a way could do that? thanks.
PHP is executed server side, so your code will include all the files listed, and send the entire output to the client computer at once.
If you want only parts of the page to appear at a time, try enclosing them in divs and use JavaScript.
Sounds like you should be using javascript to do this.
Have a look at the jQuery get method.
This might be what you're after: http://www.webresourcesdepot.com/load-content-while-scrolling-with-jquery/
Demo: http://www.webresourcesdepot.com/dnspinger/
Modify your code to look like the following
<?php
include('header.php');
?>
<div id="content">
<!-- some content -->
</div>
<div id="partOne"></div>
<div id="partTwo"></div>
<div id="partThree"></div>
<?php
include('footer.php');
?>
Then make 3 ajax requests to load in partone.php, parttwo.php and partthree.php to their respecitve divs in the above code. Plenty of tutorials on how to do this via all the different javascript frameworks out there. Personally I use jquery, but you can use whatever is your preference for this.

Categories