Forum Moderators: coopster

Message Too Old, No Replies

Problem with $var == "$variable"

         

dkin

1:01 am on Jul 29, 2004 (gmt 0)

10+ Year Member



I am still having plenty of trouble with my database.

This is the code, no matter what I do $Class always = Bard it will not loop throughthe rows in my database, it gets stuck on the first row where class = bard and thats it, I would like it to loop through and find all the rows where $class = $Class.

Anyone know whats wrong?

$results = mysql_query("SELECT * FROM nuke_quest_database",$link);
$myrow = mysql_fetch_array($results);
$Class = $myrow['Class'];
OpenTable();
print "<div align=\"center\">Click the link to <a href=\"modules.php?name=$module_name&file=submit_quest\">Submit a Quest</a>.<br><br>";

if ($Class == "$class") {
do {
print "<b><a href=\"modules.php?name=$module_name&file=quest_detail&id=$myrow[id]\">". $myrow['Race']."&nbsp;".$myrow['Class']."&nbsp;Level&nbsp;". $myrow['Level']."</a></b><br>";
}
while ($myrow = mysql_fetch_array($results));
print "</div>";
CloseTable();
}
else {

print "FIX IT $class $Class";

}
include("footer.php");

?>

[edited by: jatar_k at 4:27 am (utc) on July 29, 2004]
[edit reason] fixed sidescroll [/edit]

ergophobe

3:56 am on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$Class = $myrow['Class'];

Is not within your while loop. It is defined once, to the first value, and then never changes.

Tom

jatar_k

4:28 am on Jul 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what's the need for mysql_fetch_array twice?

dkin

4:47 am on Jul 29, 2004 (gmt 0)

10+ Year Member



ummm..... I dont know lol. which should I remove?

jatar_k

5:32 am on Jul 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about this, anew method of fixing code

you write the steps for me that you want your code to achieve and I will transform what you write into code from the code you posted. It will help you see what I am doing and allow you to be able to better understand it.

I will also get a better understanding of what exactly you are doing. The key to this is not to explain the code you wrote but to explain what you need.

It is similar to drawing flowcharts and project plans and transforming them into pseudocode before you write a program.

You know what you need to do but don't quite know how to go about it.

I know how to do it, I just don't know exactly what you are trying to do.

no rush, since i will be going to bed soon anyway. Put the appropriate amount of thought into it that it merits and I promise to honour your work with the same amount of thought in my response.

ergophobe

2:17 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Brilliant idea!

I was trying to think of something along the same lines last night but was too tired.

First organize your thoughts, then take a moment to think about how to express those thoughts clearly as you can in plain English. Once you've done that, you will be over half way to a solution.

As one possible approach, you might want to start with thinking in big chunks. What is the basic task you are trying to accomplish? What are the basic steps you think are required to accomplis that. Try an iterative process like this:

First round

Goal: show users a list of widgets

Round 2

1. get widget listing from DB
2. display listing to client

Round 3

1.1 figure out which widgets the user wants to see.
1.2 get a listing of all widgets of that type from the DB.
1.3. put that listing in alphabetical order
2.1. display that listing as an ordered list

Something like that

dkin

6:50 am on Aug 1, 2004 (gmt 0)

10+ Year Member



OK, what I am trying to do.

This is a quest database for an online adventure video game, the first screen thatI will have is a list of classes dynamically created from my class database. (this step is done)

Now I have the quest database table, When a class is clicked on the first screen I would like the user to go to a screen populated by the quests which correspond to the class they clicked on (all quests are directed toward a particular class) I would like this page to be sorted by race ascending.

When one of the quest links are clicked the detail of the quest will be shown on the page. (This step is also done)

I think that is the best I can do to explain it, please let me know if I am still confusing you.

Thank you for assistance.

dkin

4:20 am on Aug 2, 2004 (gmt 0)

10+ Year Member



Jatar any ideas?

ergophobe

5:23 am on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What do your tables look like and how are they related? No data, just the field names as in:

table quest
========
quest_id
quest_name
quest_desc

table class
===========

class_id
class_name
class_desc

table class_to_quest
===================
quest_id
class_id

etc

jatar_k

7:44 pm on Aug 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I know you said the first and third step are done but I am going to touch on them because the logic of the system requires all three steps to work together. They aren't really seperate events.

1. the first screen thatI will have is a list of classes dynamically created from my class database.

There are 2 methods of doing this

a. create links that pass the class in a query string in the url
b. create a form that will post the class to the next page

We might have something like this

$sql = "select class_id,class from mytable"; // this will give a list of all classes
$result = mysql_query($sql) or die ("<p>DB Error: " . mysql_error()); // we only use 'or die' in dev environments

now this is where are two methods would differ

a. links using $_GET

while ($row = mysql_fetch_array($result)) { 
echo '<br><a href="getquests.php?charclass=',$row['class_id'],'">',$row['class'],'</a>';
}

b. form using $_POST

echo '<form name="classselect" method="post" action="getquests.php">'; 
echo '<p><select name="charclass">';
while ($row = mysql_fetch_array($result)) {
echo '<option value="',$row['class_id'],'">',$row['class'],'</option>';
}
echo '</select><br><input type="submit" value="get quests"></form>';

So we would grab the class_id and the ckass name and use them in our display. The thing we pass is the class_id, which hopefully is represented in the quests table as a way to link the quests to each class.

2. Now I have the quest database table, When a class is clicked on the first screen I would like the user to go to a screen populated by the quests which correspond to the class they clicked on (all quests are directed toward a particular class) I would like this page to be sorted by race ascending.

the only difference for our 2 scenarios on the previous page is how we grab the value

a. links using $_GET
$sql = "SELECT * FROM nuke_quest_database where class_id=" . $_GET['charclass'] . "order by race asc";

b. form using $_POST
$sql = "SELECT * FROM nuke_quest_database where class_id=" . $_POST['charclass'] . "order by race asc";

from there everything else is the same

$result = mysql_query($sql) or die ("<p>DB Error: " . mysql_error()); 
while ($row = mysql_fetch_array($result)) {
echo '<br><a href="questinfo.php?questid=',$row['quest_id'],'">',$row['questname'],'</a>';
}

3. When one of the quest links are clicked the detail of the quest will be shown on the page. (This step is also done)

then on this page we just get the quest_id from the url and select the detailed info about that particular quest.

I used made up script names, obviously, and I don't really know the structure of your tables. You will need to have primary keys (or ids) on each of these tables and reference them from other tables. These examples are similar to what ergophobe is suggesting about your table structure above.