Php Gurus,
Look at this code. Do you see a long list of sessions ? On my membership site project, I'm having to write or copy & paste this long list of sessions on each page like home.php, users_list.php, post.php, delete_post.php, edit_post.php, etc.
Now, I want to cut short on the code.
These session variables are labeled after my mysql tbl column names. Therefore, I reckon, if I write code for the script to grab the column names and then create sessions based on the column names then the code would be cut short on each page.
I am stuck how to achieve this and so anybody's code sample would be most appreciated!
Current lengthy code that needs shortening like the way I just described:
Here's my rough attempt but I'm stuck and drowning in the muddy puddle!
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\test\grab_column_names.php on line ...
Attempt 2
Parse error: syntax error, unexpected '>' in C:\xampp\htdocs\test\grab_column_names.php on line ...
Remember, since the column names are like this:
id
username
gender
Then currently I got on my lengthy file, the sessions written like this:
$id = $_SESSION["id"];
$username = $_SESSION["username"];
$gender = $_SESSION["gender"];
I don't want to be writing lines of sessions like this to make the file size smaller and so need to write code so script fetches the column names and creates session variables under those fetched column names (or fetched labels or fetched fields).
In short, on the above mentioned lengthy code, you see a variable "$id" followed by " = $_SESSION" then followed by "['id'] again. I don't want to be typing all these 'id' and so want the script to write them by grabbing the column field name since here the column field name is 'id'. You know what I want to do. And so, care to show a code sample how to do it ? I made a few attempts and no luck.
Why did not my 1st attempt work ? See my following attempts:
I get errors on my 1st code:
Notice: Undefined index: date_&_time in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: account_activation_code in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: id_verification_video_file_url in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: password in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: passport_size_photoh_image in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: title in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: skin_complexion in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: height in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: weight in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: sexual_orientation in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: bio in C:\xampp\htdocs\test\grab_column_names.php on line 6
http://php.net/manual/en/language.variables.variable.php
Why did not my following attempts work ? See my following attempts:
Look at this code. Do you see a long list of sessions ? On my membership site project, I'm having to write or copy & paste this long list of sessions on each page like home.php, users_list.php, post.php, delete_post.php, edit_post.php, etc.
Now, I want to cut short on the code.
These session variables are labeled after my mysql tbl column names. Therefore, I reckon, if I write code for the script to grab the column names and then create sessions based on the column names then the code would be cut short on each page.
I am stuck how to achieve this and so anybody's code sample would be most appreciated!
Current lengthy code that needs shortening like the way I just described:
PHP:
<?php
//Required PHP Files.
include 'config.php';
include 'header.php';
//Check if User is already logged-in or not. Get the login_check() FUNCTION to check.
if (login_check() === FALSE)
{
//Redirect User to Log-in Page after 2 secs.
header("refresh:2; url=login.php");
exit();
}
else
{
$user = $_SESSION["user"];
$id = $_SESSION["id"];
$account_activation_status = $_SESSION["account_activation_status"];
$id_video_verification_status = $_SESSION["id_video_verification_status"];
$id_video_verification_url = $_SESSION["id_video_verification_url"];
$sponsor_username = $_SESSION["sponsor_username"];
$recruits_number = $_SESSION["recruits_number"];
$on_day_number_on_7_days_wish_list = $_SESSION["on_day_number_on_7_days_wish_list"];
$primary_website_domain = $_SESSION["primary_website_domain"];
$primary_website_email = $_SESSION["primary_website_email"];
$username = $_SESSION["username"];
$first_name = $_SESSION["first_name"];
$middle_name = $_SESSION["middle_name"];
$surname = $_SESSION["surname"];
$gender = $_SESSION["gender"];
$date_of_birth = $_SESSION["date_of_birth"];
$age_range = $_SESSION["age_range"];
$religion = $_SESSION["religion"];
$education = $_SESSION["education"];
$profession = $_SESSION["profession"];
$marital_status = $_SESSION["marital_status"];
$working_status = $_SESSION["working_status"];
$home_town = $_SESSION["home_town"];
$home_borough = $_SESSION["home_borough"];
$home_city = $_SESSION["home_city"];
$home_county = $_SESSION["home_county"];
$home_region = $_SESSION["home_region"];
$home_state = $_SESSION["home_state"];
$home_country = $_SESSION["home_country"];
}
?>
Here's my rough attempt but I'm stuck and drowning in the muddy puddle!
PHP:
<?php
//Required PHP Files.
include 'config.php';
include 'header.php';
//Check if User is already logged-in or not. Get the login_check() FUNCTION to check.
if (login_check() === FALSE)
{
//Redirect User to Log-in Page after 2 secs.
header("refresh:2; url=login.php");
exit();
}
else
{
$sql = "SHOW COLUMNS FROM browsing_histories";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
$$row['Field'] = $_SESSION["$row['Field']"]."<br>";
}
?>
Attempt 2
PHP:
<?php
//Required PHP Files.
include 'config.php';
include 'header.php';
//Check if User is already logged-in or not. Get the login_check() FUNCTION to check.
if (login_check() === FALSE)
{
//Redirect User to Log-in Page after 2 secs.
header("refresh:2; url=login.php");
exit();
}
else
{
$sql = "SHOW COLUMNS FROM browsing_histories";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
$$row['Field'] = $_SESSION["\$row['Field']\"]."<br>";
}
?>
Remember, since the column names are like this:
id
username
gender
Then currently I got on my lengthy file, the sessions written like this:
$id = $_SESSION["id"];
$username = $_SESSION["username"];
$gender = $_SESSION["gender"];
I don't want to be writing lines of sessions like this to make the file size smaller and so need to write code so script fetches the column names and creates session variables under those fetched column names (or fetched labels or fetched fields).
In short, on the above mentioned lengthy code, you see a variable "$id" followed by " = $_SESSION" then followed by "['id'] again. I don't want to be typing all these 'id' and so want the script to write them by grabbing the column field name since here the column field name is 'id'. You know what I want to do. And so, care to show a code sample how to do it ? I made a few attempts and no luck.
Why did not my 1st attempt work ? See my following attempts:
PHP:
$sql = "SHOW COLUMNS FROM users";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
${$row['Field']} = $_SESSION["{$row['Field']}"]."<br>";
}
PHP:
Notice: Undefined index: date_&_time in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: account_activation_code in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: id_verification_video_file_url in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: password in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: passport_size_photoh_image in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: title in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: skin_complexion in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: height in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: weight in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: sexual_orientation in C:\xampp\htdocs\test\grab_column_names.php on line 6
Notice: Undefined index: bio in C:\xampp\htdocs\test\grab_column_names.php on line 6
http://php.net/manual/en/language.variables.variable.php
Why did not my following attempts work ? See my following attempts:
PHP:
$sql = "SHOW COLUMNS FROM users";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
${$row['Field']} = $_SESSION["{$row['Field']}"]."<br>";
}
PHP:
$sql = "SHOW COLUMNS FROM users";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
${$row['Field']} = $_SESSION["{$row['Field']}"]."<br>";
echo ${$row['Field']}."<br>";
echo $_SESSION["{$row['Field']}"]."<br>";
}