How to record what a user clicks on - php

I'm looking to build a site which will have multiple options, for example;
NOTE** These are images not any forms or input type
Pick from one of the 5 images
Pick from one of the 3 images
Pick from one of the 2 images
Pick from one of the 6 images
and so on...
What I need to do is record which option (image) the user clicked on and then output a message.
For example if you picked a b c d, msg = hello
I'm not sure how to record this data, thanks!

You could link each image to a seperate page or reload the current page with a query string appended for example adding the following to your images as links
<img src="set1-image3.png" />
You could then save this information using PHP by processing only if the GET string contains the parameters image and set and could then split out which set/image has been clicked this way.
You could also use this to choose which image set to show on the screen (show set 2 if set=1 is in the query string etc.)
Using $_GET['set'] and $_GET['image'] you would have access to this information very easily in PHP.
For example you could do:
if( isset($_GET['image']) && isset($_GET['set']) ) {
echo "You clicked image" . $_GET['image'] . " from set " . $_GET['set'];
}
That would be the only pure PHP answer. The other way to do this is with Ajax or JavaScript but I'm guessing by your part about it being done in PHP this may not be what you want.

You could add a parameter to the link you click.
For example <img>
In page2.php you could read that variable using:
$_REQUEST["clickedImage"]
For example:
<p>The clicked image is: <?php echo $_REQUEST["clickedImage"] ?>

Related

php or jquery for dynamically created html pages?

i have over one hundred html files that all roughly have the same structure. the html files represent each page of a comic.
naturally, i've realized that it would be more efficient to apply the structure of these pages to one file, and allow that file to dynamically create all of the comic pages.
my goals are so that, one; when a user accesses a comic page, the page number that the link contained can be passed through as a number value to determine the image file and the url.
two; when a user clicks "next" or "previous", the number value that defines the page is either added to or subtracted by one to load in the new url & image file.
and three; for the "next" and "previous" buttons to define themselves/decide whether to appear or not based on a filecheck i've already written.
<?php
if ($_GET["img"]) {
$imgnum = $_GET["img"];
} else { $imgnum = 1; }
?>
<html>
<div class="block">
<?php echo '<img class="page" src="' . $imgnum . '.png">'; ?>
</div>
</html>
this code does the trick for changing the image file and url based on the value of $imgnum but when it comes to passing values into the "next" and "prev" buttons, i have no idea what the hell im doing. i tried doing THIS:
<?php $prevValue = $imgnum - 1?>
<h2 class="arrow">Previous</h2>
clearly, passing a variable into that key does not work. is there any way to preserve the dynamism of the buttons with this php method? these buttons need variables to work on their own. do i have to switch to jquery? i honestly above all else need advice on where to start. i barely understand what's going on in this php script itself.
<?php
if ($_GET["img"]) {
$imgnum = $_GET["img"];
} else { $imgnum = 1; }
?>
What the above does.....
If (the URL string contains the [img] value ) {
Get the value of [?img=] from the URL string...
i.e. index.php?img=4 .. (it is getting the 4)
and set the [$imgnum] variable to that value
} else {
But if the URL does NOT contain the [img=] value,
then set the variable value to 1 [$imgnum = 1;]
}
It's difficult to tell exactly what you want, but the below will add the value properly to the button.
<?php $preValue = $imgnum - 1; ?>
<h2 class="arrow">Previous</h2>
PHP lines end with a semicolon... and you have to echo variables for them to be output to the HTML.
I have no clue what jQuery and javascript have to do with your question. But yes.. this could all be handled by an ajax request and you wouldn't need to reload the page every time a button is clicked.
However, if you don't understand what the PHP is already doing, explaining how to do this via AJAX will be exceptionally confusing to you. And much more of your code structure would need to be seen.
You're creating the right code but you just forgot to echo it.
You need to change your line to;
<h2 class="arrow">Previous</h2>
or you can use the shorter version if your php.ini configuration allows to;
<h2 class="arrow">Previous</h2>
Note:
You should be aware of SQL injection and secure your get variables by;
$imgnum = (int)$_GET["img"];
As you are taking the $_GET value as a variable, this time, only integer values will work so hackers cannot inject a code.

PHP How to display data from database in specific sections?

I've been doing searches on how to make php output sections but couldn't find anything.
Let me explain my project and what kind of help I'm looking for exactly. I'm working on a player database that saves player results from tournament matches for a specific video game. The database will then be used by tournament commentators for pre-game analysis for the viewers. Here's the part I'm stumped on, I want the tables to be displayed in a way that's super easy and quick to find specific results.
My current setup is PHP displays data from the mySQL server in a table, which is functional but super hard to sift through, especially later on when there will be three to five more variables.
Example:
Event : -EVENT NAME-
Date : 5/19/2013
Map Names : -Map 1- -Map 2- -Map 3-
Results : -Winner- -Loser-
Event : -EVENT NAME-
Date : 5/19/2013
Map Names : -Map 1- -Map 2- -Map 3-
Results : -Winner- -Loser-
I would like to know how to make it where the information is displayed in tournament name sections, such as:
Event: -Tournament Name 1-
Event: -Tournament Name 2-
Then when you click on the specific tournament you want, it'll show you all of the results for that tournament. Could anyone point me in the right direction as to where to learn how to make this happen?
Seems like you want to display events, that expand event-related information when clicked. The jQuery UI accordion widget might be an appropriate way to handle such a data display. Have a look here:
http://jqueryui.com/accordion/
The section names shown in the demo are your events, and the text shown when they're clicked is your event text
make Event: -Tournament Name 1- a link like Tournament Name 1
then in tour.php, you check the $_GET['name'] and use that in your db query to look up the results
I'm not totally clear on how you want to achieve this but I can think of a couple options. You can use Javascript to expand a section below the Tournament Name without ever requiring the browser to do a postback, or you can simply have the tournament name linked to another page.
Option 1 (DHTML):
<script>
function showSection(tournamentID) {
document.getElementById('section' + tournamentID).style.display = 'block';
}
</script>
<?PHP
...
while ($row = ... ) {
?>
<a href="#" onclick="showSection(<?PHP echo $row['tournamentID']; ?>);return false;">
<?PHP echo $row['tournamentName'];?>
</a>
<div style="display:none" id="section<?PHP echo $row['tournamentID']; ?>">
Event: ... <br />
Date: ... <br />
Map Names: ... <br />
</div>
<?PHP
}
?>
You can use JQuery to do some cool effects, like slide down or fade in. The example above is just straight javascript and will simply show the section - but won't use any transition effects.
Option 2 (Link):
<?PHP echo $row['tournamentName'];?>
On tournamentDetails.php you would process the $_GET['id'] variable and query the database for the remaining information about that tournament and display it on the page.
To me it sounds like you want to output the data with a javascript/css accordion collapse. You can search for accordion collapse code snippets using javascript libraries like JQuery. An example of an accordion can be found on the Twitter Bootstrap site at: http://twitter.github.io/bootstrap/javascript.html#collapse
This should help alleviate the long table listing you have now, and only display the Tournament names initially unless a user wants to view more info on it, selecting the tournament to drop down more information about that tourny only.

Make a photo a link and then pass the photo id so that the next page presents information about that specific photo

This is the code. editor.php is the link. photo_id is the the information i want to pass over. $photo['picture'] is the photo.
print ('<img src='{$photo['picture']}'.''./>');
Just use session_start(); and put you link in a $_SESSION['last_link']; and use the same function on the next page and access the value you have put in $_SESSION['last_link'] to get information about the photo and display it.
PHP Sessions
Or, if you used that kind of link, in the editor.php you can use $_GET['photo_id'] to get the id of your photo, after the user have clicked on the image to follow the link.
Edit 1:
You could try putting the right concatenation:
try this:
echo '<img src='.$photo['picture'].' />';

Retain the page update by Javascript and run the PHP code?

In a php project I need to add items to database, list them & allow the user to edit & update items using a single page.
This is my code to edit item link in HTML table
echo ' Edit ';
When a user click on the above link I need to hide Add button and display two new buttons to Update & Cancel the edit + display selected item name in a Text box to Edit.
To hide and display buttons I'm using jQuery and to display the item name i need to use PHP.
Here when i put PHP code and reload the page with $_SERVER['PHP_SELF'] (as in above code), hiding & displying of buttons is lost after the page load. (If I remove the _SERVER['PHP_SELF'] code from link it hides and display buttons as expected (but no php code run))
How can I retain the page update by Javascript and run the PHP code?
I'm new to PHP am I missing something in my code?
$_SERVER['PHP_SELF'] is only a refrence to the php document itself, it doesnt contain any key/value pairs. try using:
echo '<a href=" '.$_SERVER['PHP_SELF'].'?'. $_SERVER['QUERY_STRING'].
'&name=edit&id=' .$rowCountry->CountryId .
' " id="edit" onClick="MyFunction()"> Edit </a>';
edit:
i may have misunderstood the question. you may need something like this:
<?php
if(!empty($_GET[edit])){
//echo code that u want to show AFTER they click the edit link
}else{
//echo the code to show if they have NOT clicked the edit link
}
?>

PHP URL Problem

I have a question in PHP.
I have a website thats hold news from mysql the (title, date, author, description) everything works fine, I have a page when which the user click on the title he go readmore.php?id=10 (for example)
Now my question is how to make to read the full story in one single page
for example index.php?id=10, I don't want to send in the readmore.php page, I want in one single page, how can I make this ?
check there is **id** querystring .
if(isset($_GET["id"]))
{
// show news which belongs to **id**
}
else
{
// list all news
}
But don't forgot validating $_GET["id"] variable. For example, if ids only numeric, you can check use *is_numeric();* etc.
It really depends on your current scripts but maybe you can modify the readmore.php page to only send the body of the page and then require ("readmore.php"); in your index.php where you need the full story, if the parameter (id) is the same. Then you can use something to show/hide the full body of the news in a div, in example.
you can use a jquery plugin for that, for example jQuery Expander Plugin
maybe you could send a get variable like: index.php?id=10&full=1. So, if full == 1 in index.php yo can show all the news, else cut the news with substr()
url like so:
index.php <- normal news page
index.php?readstory=10 <- will display story with id 10
in index.php
if(isset($_GET['readstory'])) {
$story_id = $_GET['readstory'];
display_story($story_id);
}
else {
display_news();
}

Categories