Jump to content

checkTable function


Lamez

Recommended Posts

I made a checkTable function, and its purpose is to find the row, and if the data is equal to 0 then show a drop down box, if not then show what is in another table, well it does neither, and I do not know why.

 

Also this is one of my first functions, ever.

 

here is the function:

<?php

   $rnd = "rnd1_win";
   $query = "SELECT * FROM `$rnd`";
   $result = mysql_query($query);
   $win = mysql_fetch_array($result);
   
   $table = "rnd1_check";
   $qu = "SELECT * FROM  `$table`";
   $re = mysql_query($qu);
   $ck = mysql_fetch_array($re);

function checkTable($num, $a, $b){   
  if ($ch['r'.$num.''] === ("0")){
    echo "<select>";
    echo '<option value="emp"></option>';
    echo '<option value="'.$a.'">'.$a.'</option>';
    echo '<option value="'.$b.'">'.$b.'</option>';
    echo '</select>';
  }else{
   echo $win[$a];
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/93463-checktable-function/
Share on other sites

Where are you pulling this variable from?

$ch['r'.$num.'']

 

You are defining variables outside of the function, then trying to use them inside the function. Instead you should pass those variables through the functions parameters, otherwise the function is only going to be good for about one use, and that defeats the entire purpose of a function.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/93463-checktable-function/#findComment-478831
Share on other sites

alright I have edited my code, and I got it to work. Then I used it again on the same page, and both places where I used the function where blank, when the values where the same, here is the new code:

 

<?php
function checkTable($num, $a, $b){ 
   $rnd = "rnd1_win";
   $query = "SELECT * FROM `$rnd`";
   $result = mysql_query($query);
   $win = mysql_fetch_array($result);
   
   $table = "rnd1_check";
   $qu = "SELECT * FROM  `$table`";
   $re = mysql_query($qu);
   $ck = mysql_fetch_array($re);
  
  if ($ck['r'.$num.''] === ("0")){
    echo "<select>";
echo '<option value="emp"></option>';
    echo '<option value="'.$a.'">'.$a.'</option>';
    echo '<option value="'.$b.'">'.$b.'</option>';
    echo '</select>';
  }else{
   echo $win['$num'];
  }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/93463-checktable-function/#findComment-478844
Share on other sites

Well you are not doing the function in a proper way u should pass the function the table name also rather then providing two tables yourself and

$ck['r'.$num.''] === ("0"))

 

it is checking only ck what abt

 

 

$win = mysql_fetch_array($result);

 

the value might be in here but in short u r using functions in a wrong way :-[

Link to comment
https://forums.phpfreaks.com/topic/93463-checktable-function/#findComment-478865
Share on other sites

Not trying to be too harsh here, but I seriously think you need to start with some basics tutorials. You have no WHERE clause in your queries which meens you are lickely to end up with more than one row returned, yet you only ever use the first row.

 

You could also likely get this done in one swift query if your intentions where a little clearer.

Link to comment
https://forums.phpfreaks.com/topic/93463-checktable-function/#findComment-478873
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.