...

View Full Version : Drop Down Menu - Displaying PHP Elements



greens85
11-01-2007, 04:09 PM
Hi All,

I currently have a Content Management System that allows the user to edit content they have already added to the system, it is displayed on this page with the following code:


Edit An Existing Article:
<br/>
<?php
$connection = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db('dbname, $connection) or die(mysql_error());
$query3 = mysql_query('SELECT COUNT(*) AS nb_messages FROM news');
$data = mysql_fetch_array($query3);
$totalDesMessages = $data['nb_messages'];

$query2 = mysql_query("SELECT * FROM news");
while ($donnees = mysql_fetch_array($query2))
{

?>
<a href="form_editnews.php?id=<?php echo $donnees[0] ?>">|<?php echo $donnees[1] ?>|</a>
<?php
}
?>
<div id="newshelp"><a href="newshelp.html" target="_blank">?</a></div>
</div>

Although this works perfectly fine, in terms of it allows the user to edit the information they have submitted. If the user adds several articles the page starts to become cluttered.

I was just wondering if it is possible to edit this articles using a drop down menu. Such as the drop down menu would contain the names of the articles that had been currently submitted and then when a user selects one from the menu, they are taken to a page where they can edit the information.

Is this possible???

Thanks in Advance

aedrin
11-01-2007, 04:10 PM
Yes, that is possible.

greens85
11-01-2007, 04:16 PM
Could you point me in the direction of a good tutorial or give me some advice, I tried something along the lines of the following but it returns blank field and doesnt link to anything:


<select name="selectnewsedit">
<?php
$connection = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db('dbcontent', $connection) or die(mysql_error());
$query3 = mysql_query('SELECT COUNT(*) AS nb_messages FROM news');
$data = mysql_fetch_array($query3);
$totalDesMessages = $data['nb_messages'];

$query2 = mysql_query("SELECT * FROM news");
while ($donnees = mysql_fetch_array($query2))
{
?>
<?php
}
?>
<option value="newsarticle1"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a></option>
<option value="newsarticle2"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a><option>
<option value="newsarticle3"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?>
</a></option>

</select>

aedrin
11-01-2007, 06:07 PM
while ($donnees = mysql_fetch_array($query2))
{
?>
<?php
}
?>



That would obviously not print out anything.

However, judging from your post it looks like you didn't paste the code properly. Just take the entire section, and put it between one set of PHP tags. Don't try to seperate the HTML from the PHP.

greens85
11-02-2007, 12:27 PM
The Code in Full Is:


Edit An Existing Article:
<br/>

<select name="selectnewsedit">
<?php
include("dbconnection.php");
$query3 = mysql_query('SELECT COUNT(*) AS nb_messages FROM news');
$data = mysql_fetch_array($query3);
$totalDesMessages = $data['nb_messages'];

$query2 = mysql_query("SELECT * FROM news");
while ($donnees = mysql_fetch_array($query2))
{
?>
<?php
}
?>
<option value="newsarticle1"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a></option>
<option value="newsarticle2"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a></option>
<option value="newsarticle3"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a></option>
</select>

<div id="newshelp"><a href="newshelp.html" target="_blank">?</a></div>
</div>

Any ideas why this isnt showing the php articles in the drop down box???

Cheers

matak
11-02-2007, 01:14 PM
they are outside the while loop

CFMaBiSmAd
11-02-2007, 02:33 PM
Sorry to be extra blunt, but does anyone read their code after they write it anymore? Writing program logic that does what you want it to do, is after all what programming means.

Aedrin pointed out that the while loop was empty and Matak pointed out that the lines that output the results are outside of the loop.

The first step in troubleshooting code is to read it to make sure it is doing what you intend. The first step in troubleshooting is not to post your code on a forum and ask some other programmer to proof read it for you. Programming help forums are for getting help with programming questions, errors, and problems. Proof reading code to make sure it is what the programmer was thinking of doing, is not what programming help forums are for. Proof reading code is not a programming question, error, or problem, it is something that the programmer who wrote the code is supposed to be doing.

matak
11-02-2007, 02:47 PM
OK, i'm troubled now couse i presumed that he can't access $donnesse variable (in this case array) outside the while loop.
Why do i think that, as there is 50% chance that i'm wrong (and i guessed i didn't test it)

#1 Case
In this case you can access array outside while loop, as it's defined outside the while loop


<?php

$i= 1;
$a = array();
while ($i <10 ) {

$a[] = $i;
$i++;
}

print_r ($a);

?>

#2 Case
You cant access array outside while loop as it is defined in while loop


<?php

$i= 1;

while ($i <10 ) {
$a = array();
$a[] = $i;
$i++;
}

print_r ($a);

?>

What i mean, is that you have array, but it's only the last number looped.

So, this code

while ($array = mysql_fetch_array($query)) {

}

behaves like #2

aedrin
11-02-2007, 03:57 PM
You are correct.

PHP's variable scope is on the function/method level, as opposed to for example Java's brace level.

So the code posted by the user will work without errors, because the $donnees variable flows down, to outside of the while loop. This is why there is no warning.

I won't ask the user why there are 3 <options> instead of just one. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum