[SOLVED] Shouldnt this function work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

[SOLVED] Shouldnt this function work?

Post by System_Failure »

Code: Select all

function frelatededit($related){
    global $r_opt;

	    $lenght = strlen($related);
		$prefex = substr($related, 0, 2);
		$relatedd_id = substr($related, 2, $lenght-2);
		
		if (($prefex == "d_") or ($prefex == "p_")) {
		  $dbtable = "table1";
		} elseif ($prefex == "g_") {
		  $dbtable = "table2";
		} elseif ($prefex == "o_") {
		  $dbtable = "table3";
		}
		
		if ($prefex == "o_") {
		  $queryd = "SELECT * FROM `".$dbtable."` WHERE id=".$relatedd_id."";
		  $resultd = mysql_query($queryd);
          $row = mysql_fetch_array($resultd);
          $r_opt = "<option value="o_".$row['id']."">".$row['value']."</option>";
		} else {
		  $queryd = "SELECT * FROM '".$dbtable."' WHERE `id`='".$relatedd_id."'";
          $resultd = mysql_query($queryd);
	      $row = mysql_fetch_array($resultd);
         $r_opt = "<option value="".$prefex.$row['id']."">".$row['name']."</option>";
	    }
		
	    return $r_opt;
	}
Basically, what I am trying to do here is pass a variable to the function called $related, and a sample value of $related would be "o_37" without the quotes.

Inside the function, php would count how many characters there are, split the prefex ( eg "o_" without the quotes) and the id ( eg "37" without the quotes )

Then depending on the prefex, choose a database table name and use that table name in the query in the next section.

In the query, it basically should fetch the data from the table chosen in the previous section, where the table id equals the splitted-from-prefex id.

Now in theory, it should output something nice like

Code: Select all

<option value="o_37">whatever-name-is-assigned-to-id-37</option>
but instead, its showing me only

Code: Select all

whatever-name-is-assigned-to-id-37
I spent about an hour going through the code, but I cant seem to find the mistake :(...may be I am just too fraustrated. If you see whats wrong with the code, please post here :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it looks like it should work.. Just a stab in the dark here, I'm going to guess you're echoing the return from this function in your browser. Make sure to check the source code if you are, as the default content type is html coming from php.. Alternately, before you start writing anything out (including html or whitespace), you could run a header call:

Code: Select all

<?php

header('Content-type: text/plain');

?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

but instead, its showing me only whatever-name-is-assigned-to-id-37
Are you sure? Do a 'view source'
You'll only see the 'proper' output if your using $r_opt inside <select></select> tags, so i'd 'view source' to check the actual output (your function works fine for me).
System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

Post by System_Failure »

Yes, I am echoing the return :p. I tried putting that header code on top of my php file, and then when I execute it, it shows me the source of the file, like the whole php file with includes and stuff.

actually, if I try to use it inbetween select tags, it gives me a mysql fetch query error for the line $resultd = mysql_query($queryd);
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Weird, echoing the return value in between select tags should affect the query at all.
I think the function is fine and the problem is where you are calling it, can you show us how you are calling the function then using the return value?
System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

Post by System_Failure »

Code: Select all

echo "<form name="news" action="index.php?action=edit" method="post">
				    <input type="hidden" name="id" value="" .$myrowdp['id']. ""> <table border="0"> <tr><td class="textbold" align="right">Name:</td><td>&nbsp;</td><td>" .ftextbox('Title', $seetitle). "</td></tr>
					<tr><td class="textbold" align="right">Poster:</td><td>&nbsp;</td><td><font color="#FF0000" class="inputtext">" .fshowusername($seeposter). "</font></td></tr>
					<tr><td class="textbold" align="right">Date:</td><td>&nbsp;</td><td><font color="#FF0000" class="inputtext">" .ftime2dateconvert($seedate). "</font></td></tr>
					<tr><td class="textbold" align="right">Related:</td><td>&nbsp;</td><td><select name='Related' class='input' size='5'>" .frelatededit($seerelated). "</select></td></tr>
					<tr><td class="textbold" align="right">News:</td><td>&nbsp;</td><td><textarea name="News" rows=10 cols=50 class="input">" .$seenews. "</textarea></td></tr>
					<tr><td class="textbold" align="right">&nbsp;</td><td>&nbsp;</td><td><input type="hidden" name="cmd" value="edit"><input type="submit" name="submit" value="Edit" size="50" class="input" onClick="return confirm('Are you sure you want to edit this news?');"></form></td></tr></table>";
frelatededit($seerelated) is the function calling :)

Sorry about the messed up code...its appearing more messy in phpbb php code :D
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

*shrug* It all works fine for me, here's the test i did:

Code: Select all

<?php
function frelatededit($related){
    global $r_opt;

       $lenght = strlen($related);
      $prefex = substr($related, 0, 2);
      $relatedd_id = substr($related, 2, $lenght-2);

      if (($prefex == "d_") or ($prefex == "p_")) {
        $dbtable = "table1";
      } elseif ($prefex == "g_") {
        $dbtable = "table2";
      } elseif ($prefex == "o_") {
        $dbtable = "table3";
      }

      if ($prefex == "o_") {
        $queryd = "SELECT * FROM `".$dbtable."` WHERE id=".$relatedd_id."";
        $resultd = mysql_query($queryd);
          $row = mysql_fetch_array($resultd);
          $r_opt = "<option value="o_".$row['id']."">".$row['value']."</option>";
      } else {
        $queryd = "SELECT * FROM '".$dbtable."' WHERE `id`='".$relatedd_id."'";
          $resultd = mysql_query($queryd);
         $row = mysql_fetch_array($resultd);
         $r_opt = "<option value="".$prefex.$row['id']."">".$row['name']."</option>";
       }

       return $r_opt;
   }
mysql_connect('localhost', '****', '****');
mysql_select_db('test');
$seerelated = 'o_37';
echo "<select name='Related' class='input' size='5'>" .frelatededit($seerelated). "</select>";
?>
The database is test and has a table called table3. That has 2 columns id, and value where id is 37 and value is 'cheese'.
The output is:
<select name='Related' class='input' size='5'><option value="o_37">cheese</option></select>
System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

Post by System_Failure »

I am doing almost the exact same as you, but how come I am getting different results?

I have four files....news.php, header.php, footer.php and functions.php

I include both header.php and functions.php in line 1 and 2 of news.php and include footer.php in the last line.

header.php connects to the database, and functions.php has the function. Footer.php closes the database connection. I echo the function just like you do, and still I am getting this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /path/functions.php on line 184
Line 184:

Code: Select all

$row = mysql_fetch_array($resultd);
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Are you sure $resultd is an actual variable handling the query?
System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

Post by System_Failure »

yes, because when I echo the function outside of <select></select>, it outputs the data just fine!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Perhaps your fetching more than once? Im not sure on that one!
System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

Post by System_Failure »

coding wise, there are two times when I am fetching, but its in an if statement, so only one.

I checked the rest of my page, functions.php, header.php and I found no other occurence of fetching $resultd :(
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

change $resultd to $result and try again please, may not work by the looks of your original code but you may have made some edits without us knowing :D
nerkn
Forum Newbie
Posts: 1
Joined: Fri Aug 06, 2004 9:16 pm
Location: batıkent

Post by nerkn »

I'm not sure but global $r_opt; could be problem, why it is global, you'r already returning a value? Also you could use " ' " in html tags. I want to post a clean version of your code:

Code: Select all

<?php
    function frelatededit($related){
    global $r_opt;

      $lenght = strlen($related);
      $prefex = substr($related, 0, 2);
      $relatedd_id = substr($related, 2, $lenght-2);
      
      if (($prefex == "d_") or ($prefex == "p_")) {
        $dbtable = "table1";
      } elseif ($prefex == "g_") {
        $dbtable = "table2";
      } elseif ($prefex == "o_") {
        $dbtable = "table3";
      }
      $queryd = "SELECT * FROM `$dbtable` WHERE id='$relatedd_id'";
      $resultd = mysql_query($queryd);
      $row = mysql_fetch_array($resultd);
     	$r_opt = "<option value='$prefex{$row['id']}'>{$row['name']}</option>";
      return $r_opt;
   } 

?>
System_Failure
Forum Newbie
Posts: 18
Joined: Thu Aug 05, 2004 11:14 pm

Post by System_Failure »

I was suggested to make it global by my friend who knows a little more about php than me :p

And I tried changing resultd to result....and tried your cleaner version and still getting that fetch query error :'(
Post Reply