Solved

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

Posted on 2012-03-28
22
413 Views
Last Modified: 2012-03-29
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
Comment
Question by:marcoullis
  • 13
  • 9
22 Comments
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37776736
sorry, disregard my previous comment, as I misinterpreted what you said.
0
 

Author Comment

by:marcoullis
ID: 37776758
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
 

Author Comment

by:marcoullis
ID: 37776765
i know... i couldn't find the reference to the click event.

:-)

-hellenica
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37776828
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
 

Author Comment

by:marcoullis
ID: 37776902
i want the expiration to occur with the next click.

will that occur in your example?

-hellenica
0
 

Author Comment

by:marcoullis
ID: 37776926
i was expecting to accomplish this task with php session variables.

is that an option.

-hellenica
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37777055
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
 

Author Comment

by:marcoullis
ID: 37777125
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
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37777153
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
 

Author Comment

by:marcoullis
ID: 37777155
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
 

Author Comment

by:marcoullis
ID: 37777159
and for retrieving the value:

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

is that correct?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37777244
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
 

Author Comment

by:marcoullis
ID: 37777352
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
 

Author Comment

by:marcoullis
ID: 37777357
i need an loop within the loop, i think.

-hellenica
0
 
LVL 7

Accepted Solution

by:
designatedinitializer earned 500 total points
ID: 37777459
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
 

Author Comment

by:marcoullis
ID: 37777526
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
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37777606
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
 

Author Comment

by:marcoullis
ID: 37777788
is there any way to reload a session variable without reloading the page?

-hellenica

ps am stuck on a detail :-(
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37777868
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
 

Author Comment

by:marcoullis
ID: 37783703
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
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37784322
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
 

Author Comment

by:marcoullis
ID: 37784691
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now