Jump to content

[SOLVED] Selection box difficulties.


Haroskyline

Recommended Posts

I seem to be having a problem getting the contents of a selection box to show up. I'm working on an internship, and we're working on a site that works with a database to show records of students in a certain program. It has already been started, but there are quite a few bugs to yet be worked out. Basically 900+ lines of code have been handed over to me, and I'm now JUST learning PHP, very basic. So, I was wondering if someone could help me with a problem we've been having.

 

<select name="sex" size="1" style="margin-left: 20px;">
<?PHP
	$result = $db->get_results("SELECT * FROM gender",OBJECT);
	foreach ($result as $key)
	{
		print "<option value=".$key->type.">".$key->desc."</option>";
	}
?>

 

<select name="schooldist" size="1" style="margin-left: 5px;">
<?PHP
$query="SELECT * FROM schools";
$sresult = $db->get_results($query,OBJECT);
foreach ($sresult as $key)
{
	print "<option value=".$key['code'].">".$key['name']."</option>";
}
?>

These two sections of code here, are supposed to access the database, and set variables to be selected in a selection box. But on the form, the selection box appears to be empty, and we can't figure out what is wrong with the code. There is this section, for determining the gender of the child, as well as a section for determining the school district they belong to. If more information is required, I will gladly answer any questions that could help you help me, and thank-you ahead of time. I'm just starting out with PHP, and hope to be on this site more often.

 

 

Link to comment
Share on other sites

1. Why did you take the internship if you just started PHP?

2. echo > print

3. use var_dump($result) to know what is in $result. Though if you're working for a company, I doubt they would make their retrieval of DB columns that tacky. They probably have a way. I bet $result is an object instead of an array.

Link to comment
Share on other sites

1. Why did you take the internship if you just started PHP?

2. echo > print

3. use var_dump($result) to know what is in $result. Though if you're working for a company, I doubt they would make their retrieval of DB columns that tacky. They probably have a way. I bet $result is an object instead of an array.

I had basic knowledge before hand, and isn't it supposed be a learning experience anyways? I can say I've learned exponential amounts already.

 

In all cases is echo better? It's what I usually use, like I said I'm supposed to be improving on someone else's code.

 

I'm looking at the code, where should I put var_dump($result)?

I don't think it would be here, would it?

$result = $db->get_results("SELECT * FROM gender",OBJECT);

 

 

Link to comment
Share on other sites

1. It's supposed to be a learning experience, but it's a bit overwhelming if you just started. There are skill levels involved too. It's like telling a newbie to work on developing Expedia. It just won't happen and that person won't last.

 

2. echo sounds way cooler! I think it's faster than print too. Though some argue it's not.

 

3. After that line.

Link to comment
Share on other sites

1. It is very overwhelming indeed, but still a great opportunity.

 

2. I think one of them allows for variables to be placed inside while the other doesn't.

 

3. Alright, I put that in, but still nothing is showing up. For some reason it's not accessing the database. I'm going to double check the table names and such, you never know. If that doesn't work, I think I found a completely different code snippet that I can work with.

Link to comment
Share on other sites

1. It is very overwhelming indeed, but still a great opportunity.

 

2. I think one of them allows for variables to be placed inside while the other doesn't.

 

3. Alright, I put that in, but still nothing is showing up. For some reason it's not accessing the database. I'm going to double check the table names and such, you never know. If that doesn't work, I think I found a completely different code snippet that I can work with.

 

Yeah, I would have started smaller, but thats just me.

 

Echo is a bit faster, but the difference is so small you won't notice a jump unless you're getting thousands of hits. The nice thing about echo is it takes in multiple parameters instead of having to concatenate everything. (e.g. echo $this, ' ', $that, (5*12), 'something';)

 

Do you know what class/methods they are using to connect to the DB?

Link to comment
Share on other sites

1. It is very overwhelming indeed, but still a great opportunity.

 

2. I think one of them allows for variables to be placed inside while the other doesn't.

 

3. Alright, I put that in, but still nothing is showing up. For some reason it's not accessing the database. I'm going to double check the table names and such, you never know. If that doesn't work, I think I found a completely different code snippet that I can work with.

 

Yeah, I would have started smaller, but thats just me.

 

Echo is a bit faster, but the difference is so small you won't notice a jump unless you're getting thousands of hits. The nice thing about echo is it takes in multiple parameters instead of having to concatenate everything. (e.g. echo $this, ' ', $that, (5*12), 'something';)

 

Do you know what class/methods they are using to connect to the DB?

 

We're using mySQL. He just came in, and we got the idea that it might be a permissions error, and sure enough it was. The schools selection box populated, now to figure out the gender selection box.

Link to comment
Share on other sites

Echo is a bit faster, but the difference is so small you won't notice a jump unless you're getting thousands of hits. The nice thing about echo is it takes in multiple parameters instead of having to concatenate everything. (e.g. echo $this, ' ', $that, (5*12), 'something';)

And apparently using commas > using dots. :) I always forget that when I use echos because I tend to use dots. Bad habits die hard. *sighs*

 

Haroskyline, KingPhilip asked you about the db class, not what database you're using.

 

And what do you mean by "one of them allows for variables to be placed inside while the other doesn't"?

Link to comment
Share on other sites

The reason I ask about the DB class, is because $db->get_results( ) is not a normal PHP method.

 

I'm going to go out on a limb and say you're probably using wordpress, with their DB interaction? If so, Id recommend turning on errors (put this before your queries):

$db->show_errors();

, or right after the query:

$db->print_error();

 

 

 

Link to comment
Share on other sites

Google ended up working out for me, finally.

 

I ended up using this code:

<select name="schooldist" size="1" style="margin-left: 5px;">
<?php
$sql = "SELECT * FROM schools";
"ORDER BY name";

$rs = mysql_query($sql);

while($row = mysql_fetch_array($rs))
{
  echo "<option value=\"".$row['code']."\">".$row['name']."\n  ";
}
?>
</select>

 

And it worked perfectly when the permissions were fixed. :D

 

Thank-you for your help guys.

 

Haha, now whenever you go to submit the information on the child, it skips right to error. Says the SSN is already taken.

 

Oh, and for the class, he said it was EasySQL.

Link to comment
Share on other sites

It worked, but if I were you, I would figure out that db class of yours because I doubt the company wants you to directly call mysql_query() when they have a class to handle these things.

I'm sorry, I don't believe I fully understand what a class is, I suppose. :\

Link to comment
Share on other sites

It's not so much as when you start but how much experience you have in doing the job you have. I was referring to the fact that you're 4-weeks into the internship and you haven't learned what a class yet.

 

I started around the age of 14, but I never really learned anything beyond the basics until 17. Of course at 14, I wasn't a hard core programmer. I was more into partying and playing sports and just having fun with friends. It's not so much of when you start but how much you know. For instance, if I were to tally up all the time I spent on PHP alone in my lifetime as of now, it wouldn't add up to more than a month total.

Link to comment
Share on other sites

No, it's not four weeks in, it's a four week internship.

I'm going on two weeks.

I'm not exactly being taught unless I run into a problem, I have to find things myself.

 

For example:

 

A Class is a blueprint for creating an Object. It tells you what variables (a.k.a. Properties) the Object can have, and what functions (a.k.a. Methods) can be used to manipulate the Object's Properties.

Link to comment
Share on other sites

A Class is a blueprint for creating an Object. It tells you what variables (a.k.a. Properties) the Object can have, and what functions (a.k.a. Methods) can be used to manipulate the Object's Properties.

Well, at least you know how to Google unlike some people here. :)

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.