need advise

For discussions about game development that does not fit in any of the other topics.
Post Reply
petermax
Posts: 37
Joined: Thu Feb 03, 2011 4:27 pm

need advise

Post by petermax »

hi, i was working on a map system and it gives me an error on line 31 , i am no expert so i was hoping someone could help me find out whats the issue

Code: Select all

01	<?php
02	require_once ("data_class.php");
03	 
04	 
05	 
06	class World extends Data {
07	 
08	var $wx = 100; // max world width
09	var $wy = 100; // max world height
10	 
11	var $qx = 10; // quadrant X
12	var $qy = 10; // quadrant Y
13	 
14	var $radius = 10; // maybe we'll need this
15	 
16	 
17	    function ShowMap() {
18	         
19	        if (isset($_POST["q"]) && isset($_POST["Change"]) && $_POST["Change"] == 'Change' && !preg_match("/[^0-9]+/", $_POST["q"]) && $_POST["q"] != '')
20	        {
21	            $q = mysql_real_escape_string($_POST["q"]);
22	        }
23	         
24	        else {
25	            $q = 1;
26	             }
27	         
28	        $sq = mysql_query("SELECT * FROM strongholds WHERE quadrant = $q");
29	         
30	        $r = 1;
31	        while ($r <= mysql_num_rows($sq)) {
32	             
33	            $d[$r] = mysql_fetch_assoc($sq); // as $d = data of that row
34	 
35	            $this->s[$d[$r][xcoo]][$d[$r][ycoo]][pid] = $d[$r][player_creatures_id];
36	            $this->s[$d[$r][xcoo]][$d[$r][ycoo]][name] = $d[$r][name];
37	             
38	             
39	            $r++;
40	        }
41	        //start of table layout and quadrant selection
42	        echo "<table width='410' cellpading='0' celspacing='0' align='center' border='0' style='margin-top:20px;'>
43	        <tr><td colspan='10'>
44	        <form action='?p=world' method='POST'>
45	        Current Region: <input type='text' value='$q' style='width:30px' maxlength='2' name='q' /><input type='submit' value='Change' name='Change' />
46	        </form>
47	        </td></tr>
48	        <tr>";
49	         
50	        $i = 1;
51	        $xi = 1;
52	        $yi = 1;
53	        while ($i <= 100) {
54	             
55	            //echo $this->s[$xi][$yi][pid];
56	             
57	        if ($this->s[$xi][$yi][pid] == $_SESSION["id"]) {
58	 
59	        echo "<td width='36' height='36' align='center' bgcolor='#339933' title='".$this->s[$xi][$yi][name]."'><img src='img/castle.gif' border='0'/></td>";  
60	             
61	        }
62	 
63	        if ($this->s[$xi][$yi][pid] != $_SESSION["id"] && $this->s[$xi][$yi][pid] != '') {
64	 
65	        echo "<td width='36' height='36' align='center' bgcolor='#339933' title='".$this->s[$xi][$yi][name]."'><img src='img/castle2.gif' border='0'/></td>"; 
66	             
67	        }
68	 
69	        else if ($this->s[$xi][$yi][pid] == ''){
70	             
71	        echo "<td width='36' height='36' align='center' bgcolor='#339933' title='[ x:$xi, y:$yi ]'><font color='#000000'></font></td>";
72	        }
73	         
74	         
75	        if ($i % 10 == 0) {echo "</tr><tr>"; $yi = $yi + 1; $xi = $xi - 10;}
76	         
77	        $i++;
78	        $xi++;
79	        }
80	        // end of table layout
81	        echo "</tr></table>";
82	         
83	    }
84	     
85	     
86	     
87	     
88	}
89	?>
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: need advise

Post by PaxBritannia »

Could we have any error messages the script gives you?

pax.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: need advise

Post by Xaleph »

Try $sql = mysql_query() or die(mysql_error());

You probably have an error in your sql query. And the query fails, hence the error in the while loop.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: need advise

Post by Jackolantern »

I think Xaleph is right, but as a sidenote, Peter, how did you get line numbers in your script? Did you write them in yourself? I am just wondering because if that is some option buried in the forums it would be good to know. It makes debugging people's scripts a lot easier.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: need advise

Post by Xaleph »

Now that i`m looking, you are first creating a loop while $r <= num_rows() however, wouldn`t it make more sense to do this:

if(mysql_num_rows($q) > 0)
{
while($r = mysql_fetch_assoc($query)){
// do something
}
}else{
// .no results were found..
}
User avatar
Gate
Posts: 25
Joined: Tue Jan 25, 2011 2:35 am

Re: need advise

Post by Gate »

Xaleph wrote:Now that i`m looking, you are first creating a loop while $r <= num_rows() however, wouldn`t it make more sense to do this:

Code: Select all

if(mysql_num_rows($q) > 0)
{
      while($r = mysql_fetch_assoc($query)){
             // do something
      } 
 }else{
   // .no results were found..
}
That logically makes no sense to me as the 'while' loops through the available data until it's at 0, hence you don't need to do

Code: Select all

if(mysql_num_rows($q) > 0)
Best to echo the sql first to see if there's any problems with the wording

then do the fore mentioned

Code: Select all

 or die(mysql_error())
if your still not having any luck.

Also you didn't post you classes include
My software never has bugs. It just develops random features.
petermax
Posts: 37
Joined: Thu Feb 03, 2011 4:27 pm

Re: need advise

Post by petermax »

yeah thanks for all the advise y think i did a mistake with the sql database also haha it is fixed now thanks for all your help guys
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

Re: need advise

Post by PaxBritannia »

No problem.

Now you just have to find a new host... Good Luck.

pax.
Post Reply

Return to “General Development”