Page 1 of 2

[SOLVED] Shouldnt this function work?

Posted: Thu Aug 05, 2004 11:14 pm
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 :)

Posted: Fri Aug 06, 2004 2:08 am
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');

?>

Posted: Fri Aug 06, 2004 5:57 am
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).

Posted: Fri Aug 06, 2004 8:33 am
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);

Posted: Fri Aug 06, 2004 8:59 am
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?

Posted: Fri Aug 06, 2004 9:04 am
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

Posted: Fri Aug 06, 2004 9:11 am
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>

Posted: Fri Aug 06, 2004 8:22 pm
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);

Posted: Fri Aug 06, 2004 8:24 pm
by Joe
Are you sure $resultd is an actual variable handling the query?

Posted: Fri Aug 06, 2004 8:29 pm
by System_Failure
yes, because when I echo the function outside of <select></select>, it outputs the data just fine!

Posted: Fri Aug 06, 2004 8:30 pm
by Joe
Perhaps your fetching more than once? Im not sure on that one!

Posted: Fri Aug 06, 2004 8:36 pm
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 :(

Posted: Fri Aug 06, 2004 8:47 pm
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

Posted: Fri Aug 06, 2004 9:16 pm
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;
   } 

?>

Posted: Fri Aug 06, 2004 10:14 pm
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 :'(