<?php
$pages = array('Text1.php', 'Text2.php', 'Text3.php', 'Text4.php', 'Text5.php');
// Track $latest in either a session variable
// $current will be dependent upon the page you're on
$latest = $_SESSION['latest'];
$current = basename(__FILE__);
$currentPages = array_search($current, $pages);
$latestPages = array_search($latest, $pages);
if ($currentPages - $latestPages > 1 ) {
?>
<div class="boxed">
Continue
<br/>
Create New
</div>
<?
} else {
// let user do their step
}
?>
I have an array which contains five pages. Now this page steps.php is externalized and stored in an an include() which is stored in 5 php pages, the same php pages stored in the array above in the array.
Now what I am trying to do is the user is suppose to follow the page structure. So if the user is on one of the pages in the array, they cannot access another page until they have submitted the page they are currently on.
But my question is that if the user click on the Continue link, how can I get the link </a> to link to the correct page so it navigates the user to the page they should be correctly on.
If you mean how you can direct the user to the correct 'next' page when they click on Continue, you can output the next page using the currentPages index + 1
<? if ($currentPages - $latestPages > 1 ) { ?>
<div class="boxed">
Continue
<br/>
Create New
</div>
<? } ?>
As much as I understand, you are trying to implement something like pagination? You should put current, previous, and next pages in the SESSION variable, so in every request you can check whether the user is in the SESSION['current'] page, and tries to go to the SESSION['previous'] or the SESSION['next'] page, also you should send a 'hiddenfield' value to the server in order to check if the user 'submitted' the page(but of course, one can simply read your html and find the 'hidden' field). Or you can simply check whether the submit button's 'name' is in the POST(or GET)? (if($_POST['NAME_OF_SUBMIT']){}) - but again it is simple to falsify, too.
Related
How to go back to a certain previous page after submitting input data in a page. I have this "edit info" button to the two different pages. This "edit info" button will be redirected to only one "Edit info" page. What I want to execute is if I am in 1st page and going to edit an info and redirect to only one "Edit info" page, after editing I will be redirected to the previous page which is the 1st page, same goes when I want to edit an info in 2nd page. I am already using 'history.go(-1)', and '(isset($_SERVER['HTTP_REFERER']))' or back and it's not working.
How will I fix this, I guess it's in my parameter or the whole condition.
'''
if($user_no > 0){
$result = $this->User_Model->update($data,$user_no);
if(isset($_SERVER['HTTP_REFERER'])) {
if($_SERVER['HTTP_REFERER'] != 'index.php/home'){
$previous_page = base_url().'index.php/home';
}else{
$previous_page = base_url().'index.php/faculty/detail/'.$user_no;
}
}
}
else{
$result = $this->User_Model->create($data,$user_no);
$previous_page = base_url().'index.php/faculty';
}
$_SESSION['result'] = ($result) ? 'SUCCESS!' : 'ERROR!';
redirect($previous_page);
'''
you need to define the path inside the base_url() function like this (only if the detail function in your controller contains an parameter!!):
$previous_page=base_url('index.php/faculty/detail/'.$user_no);
or by using <a href> tag inside your button like this will make it easier:
<a href="<?php if(condition-1)
{
echo base_url('index.php/home/')?>"
}
else { echo base_url('index.php/faculty/detail/'.$user_no)}?>"
}>Submit</a>
I forgot to state my idea of using the '$_SERVER['HTTP_REFERER']'. I just want to redirect to one of the two previous pages after editing an info. I use '($_SERVER['HTTP_REFERER'] != 'index.php/home')' condition to check if this edit info page has a previous page of either a 1st page - 'index.php/home' or 2nd page - 'index.php/faculty/detail/.$user_no'.
Hi I wrote a javascript click count its working properly but i want it to pass a single click to an external php script so as to insert the click and the id into db, it redirect a person to deals.php once a click is made on "click me" href and counts ,this is my code: Any help would be appreciated.
<script type="text/javascript">
var clicks = 0;
function linkClick()
{
document.getElementById('234').value = ++clicks;
}
document.write('<a href="deals.php?name=" + ++clicks;
onclick="linkClick()">Click Me!</a>');
</script>
You have clicked the link times.enter code here
You Can done it by using php only
on HTML Page
<?php
$query=mysql_query("select clickCount from click_count");
$result=mysql_fetch_assoc($query);
$clickCount=result['clickCount '] // get the value from database ?>
<a href="deal.php?name=<?php echo ($clickCount+1);?>" >click me</a>
on deal.php Page
let table name is click_count
$clickCount=$_GET['name'];
mysql_query("update click_count set clickCount = '".$clickCount."'");
You should not relay on client side to count the clicks. This should be done in PHP when the user clicks:
Click me
and then, on deal.php:
$id = $_GET['id'];
$clicksCount = getClicksCountForId($id);
setClicksCountForId($id, $clicksCount++);
get and set clicks count into database can be done like Shut proposed in his answer.
It's possible to simplify your code by doing everything on the PHP side and storing the clicks counter in $_SESSION. The first time it's going to be zero ($_SESSION["clicks"] = 0;), when user clicks the link, "deals.php" will be executed, and the value will be increased (and database updated), example :
main.php
<?php
session_start();
if ( ! isset( $_SESSION[ "clicks" ] ) ) // FIRST TIME.
$_SESSION[ "clicks" ] = 0;
?>
<html>
<body>
Click Me!
<input type="text" value="<?php echo $_SESSION['clicks'];?>"/>
</body>
</html>
deals.php
<?php
session_start();
$_SESSION[ "clicks" ]++; // INCREASE COUNTER.
// <=== UPDATE DATABASE HERE.
header( "Location: main.php" ); // RETURN TO MAIN.PHP.
?>
If you create two files with the given names (main.php, deals.php) and paste previous codes, it will work like you want : every click will be stored in database.
Based on Hunter F's answer, the solution to my problem is almost complete.
Just a couple of tweaks needed.
I modified the code a little and submitted a new question here at: php array help required - if current array item = 'last or first item' then 'do something'
ORIGINAL MESSAGE:
I want to be able to create a simple navigation bar with PREV and NEXT links that I can use to cycle though a list of pages. The navigation bar will be a php include within all of the pages to be cycled.
So I guess the starting point is to create an array of the pages that need to be cycled though with the PREV NEXT links.
Such as....
$projectlist = array(
'http://domain.com/monkey/',
'http://domain.com/tiger/',
'http://domain.com/banana/',
'http://domain.com/parrot/',
'http://domain.com/aeroplane/',
);
I want to option to re-order, add or remove the links. So having one self contained master array such as this seems like a logical choice to me as I'll only need to update this one list for any future additions.
Each directory being linked to has it's own index.php file, so I've left the index.php part off from the end of the links as it's not needed...or is it?
...I'm pretty stumped as of how to continue from here.
I guess I need to work out which page within the array I'm currently on, then generate the PREV and NEXT links based on that. So If I entered from 'http://domain.com/parrot/' I would need links to the relevant PREV and NEXT pages.
Any help or information to guide me on this next stage would be most appreciated.
$currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables!
$currentPath = $currentPath[0]; //grab just the path
$projectlist = array(
'/monkey/',
'/tiger/',
'/banana/',
'/parrot/',
'/aeroplane/',
);
if(! in_array($currentPath, $projectlist) ) {
die('Not a valid page!'); //they didn't access a page in our master list, handle error here
}
$currentPageIndex = array_search($currentPath, $projectlist);
if($currentPageIndex == 0) { //if it's on the first page, we want them to go to the last page
$prevlink = 'Prev';
} else { //otherwise just go to the n-1th page
$prevlink = 'Prev';
}
if($currentPageIndex == sizeof($projectlist)-1 ) { //if we're on the last page, have them go to the first page for "next"
$nextlink = 'Next';
} else {
$nextlink = 'Next';
}
One thing you may want to consider, is urlencoding the href targets in the link.
I have a small site which has 200 members. Below is code for the login/logout links which display a "Change Password", "Report" and "Logout" link when logged in. While not logged in, the "Login" and "Forgot password" links are displayed.
Recently we ran a competition which had 14 winners and what I am trying to achieve is to put a link into the code that only the 14 winners can see and not the remaining members.
I'm not quite sure where to start, is it possible to put a condition in this code for the 14 user ids/email addresses or would I be better off putting a new field into the user's database? Any help or push in the right direction would be appreciated!
<?php # loginnav.php>
// Display links based upon the login status.
// Show LOGIN links if this is the LOGOUT page.
if (isset($_SESSION['user_id'])
AND (substr($_SERVER['PHP_SELF'], -10)
!='logout.php'))
{ echo
'<li>Logout</li>
<li>Change Password</li>
<li>Report</li>
'; } else {
// Not logged in.
echo
' <li>Login</li>
<li>Forgot Password?</li>
'; } ?>
$winners_array = array('userid1', 'userid2', 'userid3', 'userid4', ...);
// This array contains users IDs who are winners
// You can write it manualy right intj the login file,
//include it from external file or form from your Data Base
if (isset($_SESSION['user_id'])
AND (substr($_SERVER['PHP_SELF'], -10)
!='logout.php'))
{
echo
'<li>Logout</li>
<li>Change Password</li>
<li>Report</li>
';
if(in_array($_SESSION['user_id'], $winners_array)){
// If current ID is in winners list we add special link for him
echo '<li>Winner link</li>';
}
} else {
// Not logged in.
echo
' <li>Login</li>
<li>Forgot Password?</li>
'; } ?>
you can simply put the winners' id in an array, then check if the user id is in that array for showing the link.
$winners = array(1, 2, 3, 4, 5);
if (in_array($id, $winners))
{
echo "link";
}
One option is to add a conditional check of the user id and if it matches your list of ids, add the link. The downside of this block of code is that it is hardcoded, and can become a maintenance issue if you plan more contests or other links unique to certain members in the future. (You would end up with several of these blocks of code)
First set your winner ids into an array, like so:
$winningIds = array(1,2,3,4,5,6,7,8,...);
Then in the echo block where you are printing the links do this:
if (in_array($_SESSION['user_id'], $winningIds))
{
echo '<li>New Link</li> ';
}
Edit: I realized I didn't mention the other option I was thinking, which is to store a list of 'unique' links in the database for each user. Then after your echo block, you'd print the unique links for each user.
I envision this as two additional tables. Table 1 would be 'links' and would have three columns - id, link and display text. Table 2 would be 'user_links' and would contain two columns - linkId and userId.
You'd join the tables and have the links (which is your href) and display text display that are associated in the user_links table.
ok, i'm trying to do a quiz...all good by now. but when i'm trying to send the collected data(radio buttons values) through pages i can't get the logic flow. I have the main idea but i can;t put it into practice.
i want to collect all radio values
create an array containing this values
serialize the array
put the serialized array into a hidden input
the problem is that i want to send data on the same page via $_SERVER['PHP_SELF'] and i don;t know when in time to do those things.(cause on "first" page of the quiz i have nothing to receive, then on the "next" page i receive the S_POST['radio_names'] and just after the second page i can get that hidden input). i hope i made myself understood (it's hard even for me to understand what my question is :D )
You could try to use the $_SESSION object instead... For each page of your quiz, store up the results in the $_SESSION array. On the summary page, use this to show your results.
To accomplish this, on the beginning of each page, you could put something like:
<?
session_start();
foreach ($_POST as $name => $resp) {
$_SESSION['responses'][name] = $resp;
}
?>
Then, on the last page, you can loop through all results:
<?
session_start();
foreach ($_SESSION['responses'] as $name => $resp) {
// validate response ($resp) for input ($name)
}
?>
Name your form fields like this:
<input type="radio" name="quiz[page1][question1]" value="something"/>
...
<input type="hidden" name="quizdata" value="<?PHP serialize($quizdata); ?>"/>
Then when you process:
<?PHP
//if hidden field was passed, grab it.
if (! empty($_POST['quizdata'])){
$quizdata = unserialize($_POST['quizdata']);
}
// if $quizdata isn't an array, initialize it.
if (! is_array($quizdata)){
$quizdata = array();
}
// if there's new question data in post, merge it into quizdata
if (! empty($_POST)){
$quizdata = array_merge($quizdata,$_POST['quiz']);
}
//then output your html fields (as seen above)
As another approach, you could add a field to each "page" and track where you are. Then, in the handler at the top of the page, you would know what input is valid:
<?
if (isset($_POST['page'])) {
$last_page = $_POST['page'];
$current_page = $last_page + 1;
process_page_data($last_page);
} else {
$current_page = 1;
}
?>
... later on the page ...
<? display_page_data($current_page); ?>
<input type="hidden" name="page" value="<?= $current_page ?>" />
In this example, process_page_data($page) would handle reading all the input data necessary for the given page number and display_page_data($page) would show the user the valid questions for the given page number.
You could expand this further and create classes to represent pages, but this might give you an idea of where to start. Using this approach allows you to keep all the data handling in the same PHP script, and makes the data available to other functions in the same script.
You want to use a flow such as
if (isset $_POST){
//do the data processing and such
}
else {
/show entry form
}
That's the most straight forward way I know of to stay on the same page and accept for data.