Re: how data from database passed to the session in a loop?
- From: TrustyWire <afzal80@xxxxxxxxx>
- Date: Fri, 17 Apr 2009 08:40:11 -0700 (PDT)
On Apr 17, 12:12 pm, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
TrustyWire wrote:
On Apr 17, 1:37 am, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:
TrustyWire wrote:
On 16 Apr, 18:38, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:Yes, people who don't know any better often make bad recommendations -
TrustyWire wrote:Jerry,
On 16 Apr, 12:35, Jerry Stuckle <jstuck...@xxxxxxxxxxxxx> wrote:My first question would be - why do you want to keep all of this in the
TrustyWire wrote:Thank you for your replies. I will try to explain and give code. here
On Apr 16, 12:02 pm, "Álvaro G. Vicario"Sorry, my crystal ball is broken. I can't see your code, your database
<alvaro.NOSPAMTH...@xxxxxxxxxxxxxx> wrote:
TrustyWire escribió:Álvaro,
i have got number of items that are already saved in the database.It's hard to suggest enhancements for a piece of code we cannot see.
When user logs in, all data is filled in using sessions.
but how can I use a loop to pass values from mysql database to the
sessions, insead of one by one typing.
the sample code to get user's values into the session is as follow:What do you mean? Is this related to the above question?
if($_POST)
{
foreach($_POST as $field => $input)
{
$_SESSION[$field] = $input;
}
ob_flush();
header("Location: form2.php");
}
--
--http://alvaro.es-Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:http://bits.demogracia.com
-- Mi web de humor al baño María:http://www.demogracia.com
--
let say i have got a CV to fill in. when user logs in back, some of
the data will be populated from the database.
currently i retrive this data using mysql statement for each field.
I wonder if there is a quicker way to do it, like using a loop.
yes, that piece of code is related to the question. I put that code,
to show that i can save inputs from the user into the session, but
cannot pull data from the database and put them in the session using a
loop.
thank you for help.
layout or anything else appropriate to the question, so I have no way to
make any suggestions.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
is the situation (assumingly):
$result = mysqli_query ($conn, "select BusinessID, UserID ,name,
activity ,address ,postcode ,phone ,website ,DescBusiness from
Business where BusinessID = '1' ") or die('Query failed.' . mysql_error
());
$row = mysqli_fetch_row($result);
$_SESSION['website'] = $row[7];
$website = $_SESSION['website'];
In fact i have got more column in the table, this is just an example.
if i want to pull all data from the table i will have quite a lot to
write and it is not what i want.
I would like to have something like this, but im not sure how to do
it?
if($row)
{
foreach($row as $field => $session)
{
$field = $_SESSION['$session'];
}
}
I would like to use output variables throught website's form pages.
thank you for your help.
$_SESSION? It can cause all kinds of problems - like what happens if
the data in the database is updated? Your $_SESSION now has old data.
It's also not a good idea to store large amounts of data in $_SESSION,
because it can slow down retrieval. Retrieving from the database is
pretty quick, especially if you store the PK in your $_SESSION and use it.
However, to answer your question -
$result = mysqli_query ($conn, "select BusinessID, UserID ,name,
activity ,address ,postcode ,phone ,website ,DescBusiness from
Business where BusinessID = '1' ");
// Also not a good idea to use die() in production code!
if ($row) {
$row = mysqli_fetch_assoc($result);
foreach ($row as $key->$value) {
$$key = $_SESSION[$key] = $value;
// You can chain assignments like this
// $$key is a variable variable.}
else {
// Gracefully handle the fetch problem here
}
The other problem with this code is that it will not be as clear as to
what it's doing, and what variables are set. Personally, I think it is
better to just write the code in individual statements. Sure, it's more
work. But it's also easier to understand:
if ($row) {
$BusinessID = $_SESSION['BusinessID'] = $row['BusinessID'];
$UserID = $_SESSION['UserID'] = $row['UserID'];
$name = $_SESSION['name'] = $row['name'];
etc...
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
Thanks for your advice and code. initially i created this
questionnaire using quite a lot of sql statements
and data was displaying quite fine, with no problems as you have
suggested using PK in the session.
But after it was done (they said, it is not robust enough), i was told
to do
it with sessions and retrieve data from the database in the beginning
and put all data into the
sessions and than use this sessions thoughtout website and than at the
end save it back to the database.
some of them even in this newsgroup. But using sessions is LESS robust
than letting the database do its job. And if you're going to save it
back to the database at the end, you'll run into HUGE problems when more
than one person is updating the same rows concurrently - data WILL get
lost your way.
I didnt know that storing large amount of data in the sessions was notAnd still a very bad idea, for multiple reasons.
a good idea. but, at least
this what i need to do.
Now, I am trying to work quick way of doing it, using foreach toIt means the argument is not an array.
retrive and save it.
And, when i try to use foreach loop to display values, it gives this
error:
Warning: Invalid argument supplied for foreach()
Timothy,We all have to learn sometime! Too bad you are working with people who
You should not say anything without being sure.
i work for a company but im not very experienced nor im a student.
maybe it is early to call me a good programmer, but I am definitely
quick learner.
seem to be clueless.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
Jerry,
Thank you for your valuable recomendations.
can you suggest what will be best practice to create these huge
questionary (8pages long, more than 150 entries)?
Is it correct to use sessions on each page and save to the database on
submit(click on next page), similarly on that page, i will pull data
from the database and put the data into the sessions(if it exists).
cheers.
I definitely would - especially with so many questions and pages.
The way you're doing it, what happens if they answer the first 17 pages
and then get a phone call? Now they're on the phone for an hour, and
when they get back their session expires. All of that data is gone and
they have to start over.
Of course, I am assuming they have some type of login or other
identifying mechanism to prevent multiple submissions. With that, it's
quite easy to restart on the last page they were on.
But I still don't understand why you are pulling data from the database
and storing it in the session. Why do you need to do this? Properly
designed, all you should need is the a logon id and possibly the current
page.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@xxxxxxxxxxxxx
==================
Jerry,
I managed to pull data out of databas and put into sessions using you
1st code.
but, how can I do vise versa, read all sessions and put into database?
I cant figure out how it's possible.
Thanks a lot.
.
- Follow-Ups:
- Re: how data from database passed to the session in a loop?
- From: TrustyWire
- Re: how data from database passed to the session in a loop?
- References:
- how data from database passed to the session in a loop?
- From: TrustyWire
- Re: how data from database passed to the session in a loop?
- From: "Álvaro G. Vicario"
- Re: how data from database passed to the session in a loop?
- From: TrustyWire
- Re: how data from database passed to the session in a loop?
- From: Jerry Stuckle
- Re: how data from database passed to the session in a loop?
- From: TrustyWire
- Re: how data from database passed to the session in a loop?
- From: Jerry Stuckle
- Re: how data from database passed to the session in a loop?
- From: TrustyWire
- Re: how data from database passed to the session in a loop?
- From: Jerry Stuckle
- Re: how data from database passed to the session in a loop?
- From: TrustyWire
- Re: how data from database passed to the session in a loop?
- From: Jerry Stuckle
- how data from database passed to the session in a loop?
- Prev by Date: Re: which method is correct ?
- Next by Date: Re: how data from database passed to the session in a loop?
- Previous by thread: Re: how data from database passed to the session in a loop?
- Next by thread: Re: how data from database passed to the session in a loop?
- Index(es):
Relevant Pages
|