Page 1 of 1

Can not get select menu to appear when in query.

Posted: Thu Jan 02, 2003 8:36 pm
by Shinmeiryu
On this control panel I have two staff levels. (1 and 2 are the supposed values). On my add data form I have option menus that determine which author posted this data, which category it's going to, etc.

I have two queries where I need a stafflevel variable to be recognized from the staff table, and likewise with author for the news table. $author needs to pick up the records from the username variable/field from the staff table as that was already queried from login activation, and it does though only with the name that I'm logged in as and not the full list. The if statement, which I'm using to determine which level of staff is logged on, doesn't pick up anything and doesn't show that selection menu for author on the value 1.

Still not sharp with fetching arrays via mysql, but I'm trying. What's off with my syntax?

Code: Select all

<?php
		$query = "SELECT * FROM news WHERE author='$author'"; 
		$result = mysql_query($query) or die('error making query'); 
		$row = mysql_fetch_array($result);
		$author=$row['author'];
		$author=$username; 
		
		$query2 = "SELECT * FROM staff WHERE stafflevel='$stafflevel'"; 
	    $result = mysql_query($query2) or die('error making query'); 
		$row = mysql_fetch_array($result);
		$stafflevel=$row['stafflevel'];
		   if ($stafflevel == 1) //is an admin, gets to select which author posted 
		   {  
		   echo "<select name='author'>"; 
		   echo "<option value='$author'>$author</option>'"; 
		   echo "</select>"; 
		   } 
		   else 
		   { 
		   echo ""; //does not have the option to choose which author 
		//submits this data, will only be submitted as the name that's logged in 
		   } 
		?>

Posted: Fri Jan 03, 2003 12:07 am
by oldtimer
I see a result for the first query but what about query2?

Posted: Fri Jan 03, 2003 12:30 am
by Shinmeiryu
Oops, pasted the wrong block of code, one I was in the middle of fixing.

The full piece is above on my original post, edited in the php quote.

Posted: Fri Jan 03, 2003 2:50 am
by phice
you're using a variable '$stafflevel' in the query line, before its even made.

Posted: Fri Jan 03, 2003 3:20 am
by Shinmeiryu
You mean in $query2? But then where would the variable $stafflevel belong at? It does have an assigned value already. Isn't the variable created once selected from the staff table in $query2?

Posted: Fri Jan 03, 2003 5:13 pm
by Shinmeiryu
Bump.

I've pretty much deleted the second query I was working on with stafflevel, and take it in smaller steps just to get the dropdown menu to show on the page, but it still won't show any of the values that should be listed.

Code: Select all

<?php
$query = mysql_query("SELECT * FROM news WHERE author='$author'");
		if(!$query)
		die(mysql_error());

		echo "<select name='author'>";
		
		while($row = mysql_fetch_array($query))
		{
		$author = $row['author'];
		$author = $username; //$username is from 'username' in staff table
		echo "<option value='$author'>$author</option>'";  
		}
		echo "</select>";
?>

Posted: Fri Jan 03, 2003 6:57 pm
by Gen-ik
I may have the wrong end of the stick here but shuoldn't you be including \ symbols in some parts of your script.. like this..

Code: Select all

<?php 

$query = mysql_query("SELECT * FROM news WHERE author='$author'") or die (mysql_error()); 

      echo ("<select name="author">"); 
       
      while($row = mysql_fetch_array($query)) 
      { 
      $author = $row[author]; 
      $author = $username; //$username is from 'username' in staff table 
      echo ("<option value="".$author."">".$author."</option>");   
      } 
      echo ("</select>"); 

?>

..it's worth a go.

Posted: Fri Jan 03, 2003 7:08 pm
by Shinmeiryu
Arent ''' only for print and not echo?

Posted: Fri Jan 03, 2003 9:02 pm
by Gen-ik
Nope.

Let's say you wanted the text "MY NAME IS BOB" (including the " symbols) to be put into the page from PHP.

You would need to use the following code otherwise PHP will get confused and think that your " symbols are part of the PHP code.. and not the text you want to display...

Code: Select all

<?php

echo(" "MY NAME IS BOB" ");

?>

..to include a variable in an echo do something like this..

Code: Select all

<?php

$myname="BOB";

echo(" "MY NAME IS ".$myname."" ");

?>

..notice the \ before any " symbol you want to be shown.


Hope this helps.

Posted: Fri Jan 03, 2003 10:30 pm
by laserlight
I dont think that it is a problem with the quotes.
Since single quotes are used within double quotes, escaping them is not needed.
It might be better to use single quotes, then double quotes in between with the variables concatenated, but that's besides the point.

Code: Select all

while($row = mysql_fetch_array($query)) 
      &#123; 
      $author = $row&#1111;'author']; 
      $author = $username; //$username is from 'username' in staff table 
      echo "<option value='$author'>$author</option>'";   
      &#125; 
      echo "</select>";
Maybe the problem lies in
$author = $username;

It seems rather silly to set $author to $row['author'], then immediately assign $username to it.
Could that be a logic error on your part?