Can not get select menu to appear when in query.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Can not get select menu to appear when in query.

Post 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 
		   } 
		?>
Last edited by Shinmeiryu on Fri Jan 03, 2003 12:31 am, edited 1 time in total.
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

I see a result for the first query but what about query2?
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Post 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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

you're using a variable '$stafflevel' in the query line, before its even made.
Image Image
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Post 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?
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Post 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>";
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Post by Shinmeiryu »

Arent ''' only for print and not echo?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
laserlight
Forum Commoner
Posts: 28
Joined: Wed Jan 01, 2003 6:41 am

Post 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?
Post Reply