Changing query variable, then executing it again - php

I am working on a PHP page that shows a table with query results. I want 2 buttons, with 'next' and 'previous', which change a variable used in this query:
SELECT les.lesuur, les.clustercode, les.lokaal, les.leraar, les.status
FROM les, zitin
WHERE zitin.leerlingnummer='$dimGebruiker'
AND zitin.clustercode=les.clustercode
AND les.dag='$huidigedag'
ORDER BY les.lesuur
$huidigedag is the one that should be changed. This is what I have in the beginning of the PHP code:
session_start();
if (!isset($_SESSION["huidigedag"])){
$_SESSION["huidigedag"] = 1;
}
$huidigedag = $_SESSION["huidigedag"];
Then, I added a link to the two buttons (arrow images):
<a href="volgende.php">
(volgende means next)
This is volgende.php:
<?php
$_SESSION["huidigedag"] = $_SESSION["huidigedag"] + 1;
header("location:leerlingrooster.php");
?>
However, when I click the button, nothing happens. I echo'd $huidigedag, and noticed it stayed on 1, without changing.

try to add session_start() to the beginning of volgende.php

I would change volgende.php to:
<?php
session_start();
$_SESSION["huidigedag"] = (!isset($_SESSION["huidigedag"])) ? 1 : ($_SESSION["huidigedag"] + 1);
header("location:leerlingrooster.php");
exit;
?>

Related

change random number when echo

i have a problem with rand function in php. i set random number on a $_SESSION and in other place echo this. but every time i echo $_SESSION, value on it is changed.
my code in page1:
session_start();
$_SESSION['y'] = rand(1,100);
echo $_SESSION['y'];
and in other page2 i write this:
session_start();
echo $_SESSION['y'];
how can i solve it?
note that page2 is appended with ajax to page1 when clicking on a button.
It sounds like you are including page 1 on page 2.
Option 1
Do not include page 1 on page 2.
Option 2
Check to see if the random number exists before setting it.
session_start();
if (! isset($_SESSION['y'])) {
$_SESSION['y'] = rand(1,100);
}
echo $_SESSION['y'];

Passing Javascript Click counts to php

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.

how to navigate user to correct page?

<?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.

php: dynamic link appends content to existing page rather than opening new one

I have a dynamic link which fetches invoice detail based on invoice ID.
<a href='<?php echo $_SERVER['PHP_SELF']; ?>/retrieve?class=InvoiceLineItems&id=<?php echo $invoice['invoice_id']; ?>'><?php echo $invoice['invoice_number']; ?></a> <?php echo $invoice['customer_name'] ?> <?php echo $invoice['invoice_date'] ?>
It calls this function
public function retrieve($class,
$id = NULL)
{
switch ($class) {
case 'Invoice':
$invoices = $this->invoice->getInvoices();
include 'view/invoiceList.php';
break;
case 'InvoiceLineItems':
$partnerInfo = $this->partnerInfo->getPartnerInfo($id);
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
include 'view/invoice.php';
break;
}
}
However, the include statement found in case 'InvoiceLineItems:' appends the content of invoice.php to the bottom of the existing page rather than replacing it altogether. I've tried adding a target to the anchor, but that didn't work. How do I get the link to open the new page?
UPDATE: based on #sixeightzero suggestion, here is the call to retrieve();
if (isset($_REQUEST['id'])) {
// A request ID value indicates arrival here through link.
$this->retrieve('InvoiceLineItems',
$_REQUEST['id']);
}
Also, I tried using a header redirect.
ob_start();
header('Location: /view/invoice.php', 302);
ob_end_flush();
exit();
It redirects, but I lose access to my array variables from
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
So, I get errors like
Notice: Undefined variable: partnerInfo in C:\xampp\htdocs\bp\view\invoice.php on line 25
and
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\bp\view\invoice.php on line 25
put exit(); at the end of the function to stop executing code after calling it.
or better still use invoice.php to display the invoices instead of the current page.
1) Since, you are using super global *$_SERVER['PHP_SELF']*, the
anchor will always direct to the same page.
2) Now, I assume, you call the function retrieve() at the end of
the page which in turn, after you hit the link, appends
'view/invoice.php' to the page, so you see the appended content at the
end.
Now, you want to get the link to open the new page, there are two ways:
1) You redirect the page to the page view/invoice.php and there you
call the function retrieve and include target="self" in the anchor tag.
Or
2) You do what you are doing but include target="blank" in the
anchor tag.
In order to get the link to open as fresh content, do as follows:
1) Add javascript to the page. In the Html Head section add the
following lines:
<script type="text/javascript">
function hideMainContentDiv(){
document.getElementById("mainContent").setAttribute("style", "display:none");
}
</script>
2) Add the onclick attribute to the anchor tag as:
<a onclick="hideMainContentDiv()">
3) Now, enclose the content of the page in and give it
id="mainContent" as:
<div id="mainContent"> <!-- All your content --> </div>
4) At the end of the page when you have enclosed your content, add
another div and call your function retrieve as:
<div id="newContent">
<?php retrieve($class,$id); ?>
</div>
What this will do is:
When you click the link, it will call the javascript and hide all your existing page content in the div whose id="mainContent".
When your function retrieve() will be called, it will include 'view/invoice.php' which will show your new content.
Your old content has already been hidden by javascript call.
I hope this resolves your query.
The answer turned out to be sessions.
case 'InvoiceLineItems':
$partnerInfo = $this->partnerInfo->getPartnerInfo($id);
$invoiceLineItems = $this->invoiceLineItems->getInvoiceLineItems($id);
$_SESSION['partnerInfo'] = $partnerInfo;
$_SESSION['invoiceLineItems'] = $invoiceLineItems;
ob_start();
header('Location: /view/invoice.php');
ob_end_flush();
exit();
break;
I added the array data to two session variables in the controller. Then, in the view, I replaced the corresponding variable names -- $partnerInfo and $invoiceLineItems -- with their session equivalents $_SESSION['partnerInfo'] and $_SESSION['invoiceLineItems'].

open random php pages from array list when I click next

I've hit a dead end. I've been working on this for 3 weeks with no result.
I have 10 php pages (1.php, 2.php, ..., 10.php) and a starting page (start.php).
All I want to do is randomize the 10 pages with no repeat, so when I click "next" in the start.php it should go to one of the 10 pages (let's say for example it goes to 4.php). When I click "next" in 4.php it should redirect to another within the 10 pages except for 4.php.
It should continue this until all the numbers (1.php - 10.php) have been displayed. At this point it should randomize again. When I click "next" in the last number .php displayed, it should randomize the number and go back to the first on the random list.
Here's what I have so far:
start.php:
<?php $vidcount = 1; ?>
<? include ("source.php"); ?>
next page
source.php:
<?php
include ("start.php");
$numbers = range(1, $total_songs);
if(($vidcount == $total_songs)||($vidcount == 1)){
shuffle($numbers);
$vidcount = 1;
}
$nextvid[1] = $numbers[0];
$nextvid[2] = $numbers[1];
$nextvid[3] = $numbers[2];
$nextvid[4] = $numbers[3];
$nextvid[5] = $numbers[4];
$nextvid[6] = $numbers[5];
$nextvid[7] = $numbers[6];
$nextvid[8] = $numbers[7];
$nextvid[9] = $numbers[8];
$nextvid[10] = $numbers[9];
?>
1.php, 2.php, ... 10.php:
<?php
include("source.php");?>
<?php echo $vidcount; ?>
next page
<?php $vidcount++;?>
1.php - 10.php have the same code. I also have a source.php which is supposed to keep track of what number has been displayed and re-shuffle when all the numbers have been displayed.
Please help. I'll greatly appreciate any help I can get.
You don't have to use the above code, I don't mind starting from scratch if you have a different idea as long as the code I get works.
Well firstly why do you have ten files when you could just have one file and ?id=X in the URL? But never mind that.
Your best bet is to use a session variable. Something like this:
<?php
session_start();
if( !isset($_SESSION['sequence']) || !$_SESSION['sequence']) {
$_SESSION['sequence'] = shuffle(range(1,10));
}
$next = array_shift($_SESSION['sequence']);
// now use $next to create your "Next page" link.
?>

Categories