Jump to content

HEREDOC and PHP Variables?


stublackett

Recommended Posts

Hi,

 

I'm trying to show a HEREDOC to match user settings, But its not outputting the relevant HEREDOC Command

 

Anyone know what needs to be altered to make it show, The menu relevant to the user level?

 

<?php 
session_start(); 
include ("dbconnect.php");

if(!isset($_SESSION[username])){ 
$message = <<<HTML
This page is for Registered users only <br /> <br />
Register to the site, <br /> <br />
By following the link in the menu on the left

HTML;

}else{ 
//Protected Area sits below
$message = "Welcome to the Dragons Den ".$_SESSION["username"]; 

} 

//Check users' User level (1 = Dragon) (2 = Entrepeneur) (3 = Admin)
mysql_connect($hostname, $db_user, $db_password)or die("cannot connect");
mysql_select_db($dbname)or die("cannot select DB");
$sql="SELECT * FROM $db_table WHERE username='" . mysql_real_escape_string($_SESSION[username]) . "";
		$result = mysql_query ($sql);
      $username = $_SESSION['username'] ;
      $userlevel = $_SESSION['userlevel'];

if($userlevel==1){
$usersettings = "1";
}else if($userlevel == 2){
$usersettings = "2";
}			      

//If Userlevel is 1 show the Entrepeneurs' Form (On the left)
if($usersettings==1){
//Entrepeneur List
$entrepeneur = <<<LEFTSIDE
<div id="login">
<b>Welcome to the site $username</b>
<ul>
<li>View Your Ideas </li>
<li>View Your Offers</li>
</ul>
</div>
LEFTSIDE;
}
//Otherwise show the Dragons' Form (On the left)
else if($usersettings==2){
//Dragon List
$dragon = <<<DRAGON
<div id="dragon">
<b>Welcome to the site $username</b>
<ul>
<li>View Latest Ideas </li>
<li>Make an Offer</li>
<li>Review Offers </li>
<li>View All Ideas</li>
<li>Manage Your Profile</li>
</ul>
</div>
DRAGON;
}
?> 

Link to comment
Share on other sites

you could simplify your document a little by using $userlevel instead of setting $usersettings.

 

 

the HEREDOC variables can should be called the same name to simplify the script also.

 

 

is your session data storing the userlevel? or are you trying to find out the userlevel from mysql? if the latter is true then the following should fixthe problem? (i went at the liberty to clean the script, hope u dont mind:P )

 

<?php

session_start(); 
include ("dbconnect.php");

if(!isset($_SESSION[username])){ 
$message = <<<HTML
This page is for Registered users only <br /> <br />
Register to the site, <br /> <br />
By following the link in the menu on the left

HTML;

}else{ 
  //Protected Area sits below
  $message = "Welcome to the Dragons Den ".$_SESSION["username"]; 

} 

//Check users' User level (1 = Dragon) (2 = Entrepeneur) (3 = Admin)
mysql_connect($hostname, $db_user, $db_password)or die("cannot connect");
mysql_select_db($dbname)or die("cannot select DB");

// changed * to `userlevel` to only get needed data (rather than username/id and userlevel in an array you get one string per array/row)
$sql="SELECT `userlevel` FROM $db_table WHERE username='".mysql_real_escape_string($_SESSION[username]);
		$result = mysql_query($sql);
      $username = $_SESSION['username'];
      $userlevel = mysql_result($result,0);	// gets userlevel from mysql result		      

//If Userlevel is 1 show the Entrepeneurs' Form (On the left)
if($userlevel=="1"){
//Entrepeneur List
$page = <<<LEFTSIDE
<div id="login">
<b>Welcome to the site $username</b>
<ul>
<li>View Your Ideas </li>
<li>View Your Offers</li>
</ul>
</div>
LEFTSIDE;
}
//Otherwise show the Dragons' Form (On the left)
else if($userlevel=="2"){
//Dragon List
$page = <<<DRAGON
<div id="dragon">
<b>Welcome to the site $username</b>
<ul>
<li>View Latest Ideas </li>
<li>Make an Offer</li>
<li>Review Offers </li>
<li>View All Ideas</li>
<li>Manage Your Profile</li>
</ul>
</div>
DRAGON;
}

echo($page);
?> 

Link to comment
Share on other sites

Muchos Gracias! As the Spaniards say

 

Codes following the principles I have and yes the Session ID of 1 or 2 is being output, I checked that with an echo statement

 

BUT That SQL Statement is causing a bit of trouble

 

Warning: Wrong parameter count for mysql_result() on line 27

 

Line 27 being the new SQL Select you put in for me

 

$userlevel = mysql_result($result,0); // gets userlevel from mysql result

 

 

Link to comment
Share on other sites

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 27

 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource on line 28

 

Thing is, Its already got the Userlevel selected, So maybe that code doesnt need to be used?

Link to comment
Share on other sites

what exactly are you pulling from the mysql database?

 

 

if the userlevel is ONLY stored in mysql try this:

<?php

session_start(); 
include ("dbconnect.php");

if(!isset($_SESSION['username'])){ 
$message = <<<HTML
This page is for Registered users only <br /> <br />
Register to the site, <br /> <br />
By following the link in the menu on the left

HTML;

}else{ 
  //Protected Area sits below
  $message = "Welcome to the Dragons Den ".$_SESSION['username']; 

} 

//Check users' User level (1 = Dragon) (2 = Entrepeneur) (3 = Admin)
mysql_connect($hostname, $db_user, $db_password)or die("cannot connect");
mysql_select_db($dbname)or die("cannot select DB");

// changed * to `userlevel` to only get needed data (rather than username/id and userlevel in an array you get one string per array/row)
$sql="SELECT `userlevel` FROM $db_table WHERE username='".mysql_real_escape_string($_SESSION['username']);
		$result = mysql_query($sql) or die("QUERY ERROR:<br>".mysql_error());
      $username = $_SESSION['username'];
      $userlevel = mysql_result($result,0);		      

//If Userlevel is 1 show the Entrepeneurs' Form (On the left)
if($userlevel=="1"){
//Entrepeneur List
$page = <<<LEFTSIDE
<div id="login">
<b>Welcome to the site $username</b>
<ul>
<li>View Your Ideas </li>
<li>View Your Offers</li>
</ul>
</div>
LEFTSIDE;
}
//Otherwise show the Dragons' Form (On the left)
else if($userlevel=="2"){
//Dragon List
$page = <<<DRAGON
<div id="dragon">
<b>Welcome to the site $username</b>
<ul>
<li>View Latest Ideas </li>
<li>Make an Offer</li>
<li>Review Offers </li>
<li>View All Ideas</li>
<li>Manage Your Profile</li>
</ul>
</div>
DRAGON;
}

echo($page);
?> 

 

If userlevel is ALREADY stored in a SESSION variable this should work:

 

<?php

session_start(); 
include ("dbconnect.php");

if(!isset($_SESSION['username'])){ 
$message = <<<HTML
This page is for Registered users only <br /> <br />
Register to the site, <br /> <br />
By following the link in the menu on the left

HTML;

}else{ 
  //Protected Area sits below
  $message = "Welcome to the Dragons Den ".$_SESSION['username']; 

} 

$username = $_SESSION['username'];
$userlevel = $_SESSION['userlevel'];	      

//If Userlevel is 1 show the Entrepeneurs' Form (On the left)
if($userlevel == "1"){
//Entrepeneur List
$page = <<<LEFTSIDE
<div id="login">
<b>Welcome to the site $username</b>
<ul>
<li>View Your Ideas </li>
<li>View Your Offers</li>
</ul>
</div>
LEFTSIDE;
}
//Otherwise show the Dragons' Form (On the left)
else if($userlevel == "2"){
//Dragon List
$page = <<<DRAGON
<div id="dragon">
<b>Welcome to the site $username</b>
<ul>
<li>View Latest Ideas </li>
<li>Make an Offer</li>
<li>Review Offers </li>
<li>View All Ideas</li>
<li>Manage Your Profile</li>
</ul>
</div>
DRAGON;
}

echo($page);
?> 

the last code removes the mysql query, since it serves no purpose if you already have the userlevel.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.