php / mysql

Permabanned
Joined
19 Apr 2006
Posts
2,333
Location
West Yorkshire
Does this look right?

Code:
<?php
$names = @mysql_query( 'SELECT name FROM sed_1car1_branches' );
?>

<table>
	<tr>
		<td>
			<ul>
				<? echo('<li><a href="#' . $names . '">' . $names . '</a></li>'); ?>
</ul>
		</td>
	<tr>
</table>

I am n00b and the php stuff.
 
Last edited:
How many rows are you expecting back from the SQL?

As far as I see, your getting the result from the SQL in a variable called $names then calling the raw SQL result.

Try adding
Code:
$row = mysql_fetch_row($result);

Then change this line
Code:
<? echo('<li><a href="#' . $names . '">' . $names . '</a></li>'); ?>

to this.

Code:
<? echo('<li><a href="#' . $row . '">' . $row . '</a></li>'); ?>
 
I think you might want something along the lines of

Code:
<?php
$query = "SELECT name FROM sed_1car1_branches";
$result = mysql_query($query) or die(mysql_error());
$nor = mysql_num_rows($result);

if($nor >= 1)
{
?>
<table>
<tr>
<td>
<ul>
<?php
while($row = mysql_fetch_assoc($result))
{
$name = $row["name"];
?>
<li><a href="#<?php echo $name; ?>"><?php echo $name; ?></a></li>
<?php
}// while($row = mysql_fetch_assoc($result))
?>
</ul>
</td>
<tr>
</table>
<?php
}//if($nor >= 1)
?>
 
thanks for the help, it appears my code was all screwed up completely so I redid it like this:

Code:
$query="SELECT * FROM sed_1car1_branches ORDER BY name ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;


echo "<ul>";
while ($i < $num) {

$name=mysql_result($result,$i,"name");

echo "<li><a href='#$name'>$name</a></li>";
$i++;
}
echo "</ul>";

As I say I am a bit of a n00b at all this, but am getting there.

I would like to change it now, so instead of outputting a huge list, it instead gives you a drop down list with all the names in.

How do go I about that?
 
Code:
<?php
if($_POST['go']) {
    $url = $_POST['url'];
    header("Location: $url");
}
?>
<html>
<head></head>
<form action="" method="post" name="f">
<select name="url">
<?php
$result = mysql_query("SELECT name FROM sed_1car1_branches ORDER BY name ASC");
while($row = mysql_fetch_assoc($result)) {
    $name = $row['name'];
    echo '<option value="'.$name.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" name="go" value="Go!">
</form>
</body>
</html>

tidied it up a bit. :)

edit: didn't realise the functionality that was needed from the drop down list so have updated... :p

edit2: cleaning up a bit...

edit3: more typos...
 
Last edited:
The_KiD said:
I would like to change it now, so instead of outputting a huge list, it instead gives you a drop down list with all the names in.

How do go I about that?

Code:
<?php

$query="SELECT * FROM sed_1car1_branches ORDER BY name ASC";
$result=mysql_query($query);

?>
<select name="ListOfSomething" onchange="if (this.value != '') document.location = (this.value);">
<?php
$first_row = TRUE;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			if($first_row === TRUE) {
				?>
				<option selected value="#<?php echo $name; ?>"><?php echo $name; ?></option>
				<?php
				$first_row = FALSE;
			} else {
				?>
				<option value="#<?php echo $name; ?>"><?php echo $name; ?></option>
				<?php
			}
	}
?>
</select
<?php
//more code

I didn't test this seeing as I don't have a database, expect a few small errors :)
 
marc2003 said:
<snip>

tidied it up a bit. :)

Thanks dude, that works a treat.

However I want it so when one of the names is selected it then refreshes the page and displays the rest of the info?
 
The_KiD said:
Thanks dude, that works a treat.

However I want it so when one of the names is selected it then refreshes the page and displays the rest of the info?

see my edit above, i missed the whole link thing that was required.. :p

edit: i'm assuming the results are urls and you want to go a page when you choose from the list? i really should pay more attention before jumping in...
 
Last edited:
Almost marc :P I appreciate the help though.

Let me explain properly what I am doing, I am trying to make a staff intranet and am using the Seditio CMS system, which is working great.

Now I need to convert some spreadsheets and documents into web friendly pages and forms.

This page in particular is instead of a spreadsheet that contains all our branch information.

So what I am wanting is for them to come to this page, select the branch name they are looking for, then the page refreshes (still with the select box at the top) and displays the info for the branch they selected.

It has to stay on the one page, not multiple pages and it cant contain naked html (as it needs to be encapsulated in php) as the CMS allows parsing of html or PHP not both :S
 
right, aren't you going to need another query for this. the first one displays a list of branches. you need to query the database again to get the branch information. otherwise, where's it coming from? :)
 
Is it not possible to do this (and I appologise for the daft psuedo code type writing).

if VALUE is set
THEN
display branch_info
ELSE
display select_box
IF SUBMIT
THEN
VALUE = branch_name

if that makes any sense?
 
Code:
<html>
<head></head>
<form action="" method="post" name="f">
<select name="branch">
<?php
$result = mysql_query("SELECT name, branch_info FROM sed_1car1_branches ORDER BY name ASC");
while($row = mysql_fetch_assoc($result)) {
    $name = $row['name'];
    if($_POST['branch'] == $name) $branch_info = $row['branch_info'];
    echo '<option value="'.$name.'">'.$name.'</option>';
}
?>
</select>
<input type="submit" name="go" value="Go!">
</form>
<br>
<?php echo $branch_info; ?>
</body>
</html>

maybe? :)
 
Thanks again Marc, it is genuinely appreciated.

I had to play around a bit with the code, but this is how I got it in the end;

Code:
echo '
<form action="" method="post" name="f">
<select name="branch"> ';


$result = mysql_query("SELECT * FROM sed_1car1_branches ORDER BY name ASC");

while($row = mysql_fetch_assoc($result)) {
    $name = $row['name'];
    if($_POST['branch'] == $name)  { 
		$bname = $row['name'];
		$baddress = $row['address']; 
		$bpcode = $row['post_code']; 
		$bcode = $row['code']; 
		$btel_front = $row['tel_front']; 
		$btel_back = $row['tel_back']; 
		$bfax = $row['fax']; 
		$bmobile = $row['mobile']; 
		$bmanager = $row['manager']; 
		$bemail = $row['email'];
 }
else {
    echo '<option value='.$name.'>'.$name.'</option>' ;} 
}

echo '
</select>
<input type="submit" name="go" value="Go!">
</form>
<br /> ';

if ($baddress) {
echo " <table>
			<tr>
				<td colspan='2'><strong>$bname ( $bcode )</strong></td>
			</tr>
			<tr>
				<td><strong>Manager:</strong></td>
				<td>$bmanager</td>
			</tr>
			<tr>
				<td><strong>Address:</strong></td>
				<td> $baddress , $bpcode</td>
			</tr>
			<tr>
				<td><strong>Front Line:</strong></td>
				<td>$btel_front</td>
			</tr>
			<tr>
				<td><strong>Back Line:</strong></td>
				<td>$btel_back</td>
			</tr>
			<tr>
				<td><strong>Mobile:</strong></td>
				<td>$bmobile</td>
			</tr>
			<tr>
				<td><strong>Fax:</strong></td>
				<td>$bfax</td>
			</tr>
			<tr>
				<td><strong>E-mail:</strong></td>
				<td><a href='mailto:$bemail'>$bemail</a></td>
				</tr>
			</table> ";
}

The only thing I can't do now is make it so, when a branch is selected it's name stays in the selection box when the page refreshes, instead it reverts back to the first in the list.

Is this possible?
 
Indeed. Add the word SELECTED to the <option> tag within the <select>.

For example, if you want oranges,

Code:
<SELECT NAME="fruit">
    <OPTION VALUE=apples>Apples</option>
    <OPTION VALUE=oranges SELECTED>Oranges</option>
    <OPTION VALUE=pears>Pears</option>
</SELECT>

EDIT: Oops... I copied and pasted that from an HTML 3 page. haha. Do what flibby said. :)
 
Last edited:
SELECTED is kinda bad practice, so is anything in uppercase, and would fail XHTML validation. You should write:

selected="selected"

or

selected="true"

Just a pointer.
 
you need to lose the else bit? your script is not adding the selected branch to the list at the moment because you're telling it not to. :p

Code:
echo '<option value='.$name;
if($_POST['branch'] == $name) {
    //assign variables
    echo ' selected="selected"'
}
echo '>'.$name.'</option>';

:)
 
Absolutely fantastic marc :) \o/ Is working like a treat !

final thing though I promise.

Any branch name ($name) with a space in it like "Head Office" or "Milton Keynes" and it doesnt work, it takes you back to the selection box with no info for that branch.

If I select "Manchester Airport" or "Manchester Central" then it takes me to the details of the "Manchester" site, so it looks like it isnt taking the second word in to account.
 
marc2003 said:
you'll need to add double quotes around the value....

Code:
echo '<option value="'.$name.'"';


Now I deffinitely owe you a beer or 10 :)

Thanks all, but especially your good self Mr Marc, no doubt I will be back with more questions.
 
Code:
if ($baddress) {
echo " <table>
			<tr>
				<td colspan='2'><strong>$bname ( $bcode )</strong></td>
			</tr>
			<tr>
				<td><strong>Manager:</strong></td>
				<td>$bmanager</td>
			</tr>
			<tr>
				<td><strong>Address:</strong></td>
				<td> $baddress , $bpcode</td>
			</tr>
			<tr>
				<td><strong>Front Line:</strong></td>
				<td>$btel_front</td>
			</tr>
			<tr>
				<td><strong>Back Line:</strong></td>
				<td>$btel_back</td>
			</tr>
			<tr>
				<td><strong>Mobile:</strong></td>
				<td>$bmobile</td>
			</tr>
			<tr>
				<td><strong>Fax:</strong></td>
				<td>$bfax</td>
			</tr>
			<tr>
				<td><strong>E-mail:</strong></td>
				<td><a href='mailto:$bemail'>$bemail</a></td>
				</tr>
			</table> ";
}
Think this whole section could be bad, as your giving PHP variable names inside a echo function without breaking it to print the variable. Such as:

Code:
echo "This variable: $whatever, is going to be printed as text";
instead of:

Code:
echo "This variable: " . $whatever . ", isn't going to be printed as text";
 
Back
Top Bottom