Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

PHP / MySQL add session variable to Do/While loop when link is clicked by user

I want to set a session variable equal to the placement in the hierarchical list produced by this do/while loop. in other words the 3rd entry in the list shall set the session variable to 3 when that link is clicked.

<?php do { ?>
                                    
                                    <li class="contact clearfix">
                                    <div class="avatar"><img src="http://some.website.net/uploads/img/<?php echo $row_rs_last10['image']; ?>" width="32" /></div>
                                    <a class="more" href="reader_last10.php">&raquo;</a> <span class="timestamp"></span> 
                                    <div font-family: Arial Black;><a href="#" class="name"><?php echo $row_rs_last10['subject']; ?></a></div>
                                    <div class="entry-meta">Article: <?php echo $row_rs_last10['articleID']; ?> published <?php echo $row_rs_last10['publishDate']; ?> </div>
                                    </li>
                                    
                                    <?php } while ($row_rs_last10 = mysql_fetch_assoc($rs_last10)); ?>

Open in new window

0
marcoullis
Asked:
marcoullis
  • 13
  • 9
1 Solution
 
designatedinitializerCommented:
sorry, disregard my previous comment, as I misinterpreted what you said.
0
 
marcoullisAuthor Commented:
and your proposed solution works with clicking on the link?

i need the session variable to be set when the link is clicked at line 7.

please confirm before i proceed.

-hellenica
0
 
marcoullisAuthor Commented:
i know... i couldn't find the reference to the click event.

:-)

-hellenica
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
designatedinitializerCommented:
You can set the cookie using javascript.
jQuery is better, 'cause it's cleaner-looking.
Like this:
$.cookie("test", 1, {
   expires : 10,           //expires in 10 days

   path    : '/',          //The value of the path attribute of the cookie 
                           //(default: path of page that created the cookie).

   domain  : 'jquery.com',  //The value of the domain attribute of the cookie
                           //(default: domain of page that created the cookie).

   secure  : true          //If set to true the secure attribute of the cookie
                           //will be set and the cookie transmission will
                           //require a secure protocol (defaults to false).
});

Open in new window


you can put that inside a function and call that function onClick.
0
 
marcoullisAuthor Commented:
i want the expiration to occur with the next click.

will that occur in your example?

-hellenica
0
 
marcoullisAuthor Commented:
i was expecting to accomplish this task with php session variables.

is that an option.

-hellenica
0
 
designatedinitializerCommented:
you have to understand that PHP works on the server side, JS on the client (browser) side.
You use session variables to maintain state, because a web page is stateless.

You can never have a link that sets a PHP variable.
You can, however, have a link like this:
<a href="index.php?variable=something">Click me!</a>

Open in new window

and then have that PHP page look for that GET variable, and place it safely in a session, like this:
<?php
if(isset($_GET['variable'])){
        $_SESSION['sessionvarname'] = $_GET['variable'];
}
?>

Open in new window


You can also accomplish a similar task by setting a cookie in JavaScript, and then reading the cookie in PHP.
But keep in mind that PHP only 'kicks in' when the server gets a request from the browser (i.e., after the user clicks a link)
0
 
marcoullisAuthor Commented:
that is exactly what I was looking for... gonna start putting together your solution and will get back to you asap. thank you for having so much patience with me.

-hellenica
0
 
designatedinitializerCommented:
don't forget to put

session_start();

At the very top of your script. And you can't have any whitespace before the php opening tag (<?php).
0
 
marcoullisAuthor Commented:
how do I label the variable in the URL? like this:

<a href="index.php?$_SESSION['var_THIS_articleID']=7">Click me!</a>

and if so, how do i increment the variable value?

i need the value to increment by 1 for each loop of the do/while function.

please advise.

-hellenica
0
 
marcoullisAuthor Commented:
and for retrieving the value:

<?php
if(isset($_GET['variable'])){
        $_SESSION['var_THIS_articleID'] = $_GET['variable'];
}
?>

is that correct?
0
 
designatedinitializerCommented:
wrong...
when you say:
 $_SESSION['var_THIS_articleID']
You are retrieving the variable. This will be substituted by PHP for the contents of that session variable.
So... the links will be, for instance:

<...>
<a href="index.php?x=7">Click me!</a>
<a href="index.php?x=8">Click me!</a>
<a href="index.php?x=9">Click me!</a>
<a href="index.php?x=10">Click me!</a>
<...>

Open in new window


then you retrieve the value as $_GET['x']
If you want to store it in the session, you can store it with another name, like this:

 $_SESSION['var_THIS_articleID'] = $_GET['x'];

Open in new window


get it?
GET variables are those that go through in the url, after the "?"
SESSION variables are a different thing. These will persist from page load to page load.
0
 
marcoullisAuthor Commented:
but the increments have to be within the loop.

you have them static in your example.

in my script the x=some number has to be added during the loop.

see what i mean.

its missing the iteration.
0
 
marcoullisAuthor Commented:
i need an loop within the loop, i think.

-hellenica
0
 
designatedinitializerCommented:
no, you just need a variable that expresses that value, and then increment that variable.
Like this:
<?php 
$my_counter = 1;
do { ?>
                                    
                                    <li class="contact clearfix">
                                    <div class="avatar"><img src="http://some.website.net/uploads/img/<?php echo $row_rs_last10['image']; ?>" width="32" /></div>
                                    <a class="more" href="reader_last10.php">&raquo;</a> <span class="timestamp"></span> 
                                    <div font-family: Arial Black;><a href="index.php?x=<?php echo $my_counter++;?>" class="name"><?php echo $row_rs_last10['subject']; ?></a></div>
                                    <div class="entry-meta">Article: <?php echo $row_rs_last10['articleID']; ?> published <?php echo $row_rs_last10['publishDate']; ?> </div>
                                    </li>
                                    
                                    <?php } while ($row_rs_last10 = mysql_fetch_assoc($rs_last10)); ?>

Open in new window


The '++' after the variable name is what increments the variable.
0
 
marcoullisAuthor Commented:
i got it to work with the link, but now the problem is i need to get a button submit to do what my link just did. is there a way to do that?

-hellenica
0
 
designatedinitializerCommented:
you add an hidden input field with the value you need to pass along to the php script.
Like this:
<input type="hidden" name="x" value="123" />

and don't forget about the form tag, before all that:
<form name="formname" method="get" action="index.php">

..and close the form at the bottom of the page:
</form>

The above hidden field example will set $_GET['x'] to "123"
0
 
marcoullisAuthor Commented:
is there any way to reload a session variable without reloading the page?

-hellenica

ps am stuck on a detail :-(
0
 
designatedinitializerCommented:
I'm afraid I can't help you any further because you fail to understand the basic principles of the server-client architecture in which the WWW is based.
PHP runs on the server. The browser must send the information to the server.
This means there must be a REQUEST.
HTML is 'dead': it is motionless, stateless.
The only thing you can do besides, is to set cookies in javascript, and THEN read them in PHP.
Either way, there ALWAYS must be a REQUEST to the server at some point.
You are NOT developing a computer program.
You are developing a web application which has a piece in the browser (HTML + js) and the other pieces on the server (PHP, database, etc). The two must communicate.

I advise you to get help from a pro, or get basic web development training.
0
 
marcoullisAuthor Commented:
That last comment was uncalled for.

None of the people making requests on EE
are experts, that's why we are here.



Regardless, i got it to work.

-hellenica
0
 
designatedinitializerCommented:
I'm really sorry if I upset you. That wasn't my intent, I guarantee.
I say you need some trainning because it is true. There's nothing to be ashamed of or upset about.
It's just that there are concepts which are very hard to get through just by exchanging text messages like we are doing here. If you get someone to explain these concepts to you in person you will go "aha! That's what that guy was going on about!"
I'm glad you actually got it to work and proved you could do it.
0
 
marcoullisAuthor Commented:
Look i don't know everything about PHP, or Java, or MySQL or Obective-C but I have worked with all these languages and have been quite successful. That includes programming for the web since 1999, for Windows since 2010 and iOS since last year. If it appears from my texts here that I miss the fundamentals that is a misunderstanding, not the case.

Please also keep in mind that when we chatted I was without sleep for 2 days trying to finish a module for a client. So don't rush to judge.

Regardless your help was invaluable. I'm just suggesting that the gap between expert and experienced is your bread and butter and perhaps your tone should be less assumptive and more about nurturing.

I like this service very much, and consider it a value added proposition that makes sense.

kind regards,

-hellenica
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now