PHP - While Loop Calling Function Containing Query
I've created a function getsub(). The idea is that this function contains a query that grabs records from the categories table, using the $row['cat_id'] that I pass to it from within an existing while ($row=mysql_fetch_assoc($res)) loop.
Heres the function: function getsub($rowid,$catsort) { global $system, $LANGUAGES, $subres; $subquery = "SELECT * FROM webid_categories WHERE parent_id = " . $rowid . " " . $catsort; $subres = mysql_query($subquery); $system->check_mysql($subres, $subquery, __LINE__, __FILE__); return $subres; } And heres the code that calls this function and passes the $row['cat_id'] value to it: // prepare categories list for templates/template // Prepare categories sorting if ($system->SETTINGS['catsorting'] == 'alpha') { $catsorting = ' ORDER BY cat_name ASC'; } else { $catsorting = ' ORDER BY sub_counter DESC'; } $query = "SELECT cat_id FROM " . $DBPrefix . "categories WHERE parent_id = -1"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); $query = "SELECT * FROM " . $DBPrefix . "categories WHERE parent_id = " . mysql_result($res, 0) . " " . $catsorting . " LIMIT " . $system->SETTINGS['catstoshow']; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); while ($row = mysql_fetch_assoc($res)) { $subcats = getsub($row['cat_id'],$catsorting); while($subrow = mysql_fetch_assoc($subcats){ $template->assign_block_vars('sublist', array( 'SID' => $subrow['cat_id'], 'SNAME' => $category_names[$getrow['cat_id']] )); } $template->assign_block_vars('cat_list', array( 'CATAUCNUM' => ($row['sub_counter'] != 0) ? '(' . $row['sub_counter'] . ')' : '', 'ID' => $row['cat_id'], 'IMAGE' => (!empty($row['cat_image'])) ? '<img src="' . $row['cat_image'] . '" border=0>' : '', 'COLOUR' => (empty($row['cat_colour'])) ? '#FFFFFF' : $row['cat_colour'], 'NAME' => $category_names[$row['cat_id']] )); } then a .tpl file just references the {sublist.SID} and {sublist.SNAME} variables. BUT.... It ain't working. My page just goes blank. Any help would be massively appreciated. Similar TutorialsThanks in advance for help. I have a PHP script that has a function called traverseXMLNodes and it passes an xml string. I want anyone with a username and password to be able to call this function from his website or application or programming. In short i want my site to act as an API(Application Programming Interface). I am really stuck as to how to go about this. There are other functions i would like to call as well so traverseXMLNodes is not the only function i want to my "API" users to access. Below is the code I am using. the filename for my php i called remote.php. I am using the following query string to test and no results are returned. http://localhost/testfolder/remote.php?xmlstring=20 Code: [Select] <?php $allowedfuncs = array('xmlstring','remotefunction2'); if(in_array($_get['xmlstring'], $allowedfuncs)) { $func = $_get['xmlstring']; $output = traverseXMLNodes($func); // This calls $_get['funccall']() echo $output; } else { exit(); } function traverseXMLNodes ($func) { echo "The XML string is"; echo "<br/>"; echo $func; echo "<br/>"; } /* More functions go here */ ?> Hello, New to PHP and trying to loop through rows to and get the name of the "property" in this case. I have tried foreach and while and neither seem to want to return all rows by calling the function. Does it have something to do with echo'ing the table structure or can I not return an array out of a function like this? I commented out the while loop as that wasn't working and put in a foreach loop though that didn't return all the data either. Any help is greatly appreciated Code: [Select] <?php function getProperties(){ $sql = "SELECT propertyName FROM properties;"; $result = mysql_query($sql); //$count = mysql_num_rows($result); $property = mysql_fetch_array($result); foreach($property as $propertyName){ echo "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" name=\"$propertyName\" \"id=\"$propertyName\" />$propertyName</div></td>"; } /* for($i=0;$i>=$count;$i++){ while($property = mysql_fetch_array($result)){ $propertyName = $property['propertyName']; echo "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" name=\"$propertyName\" \"id=\"$propertyName\" />$propertyName</div></td>"; } } */ } ?> Hi All, I am working on system where i am storing column names in one table of database. To retrieve value of columns with separation of comma (exp. clol1, col2,col3..). for this i am using one function which i am calling middle of mysql query. but i am not getting fetch result over there. Below is the function i have to fetch column names. Code: [Select] function retrieve_columns() { $brand_id = $_SESSION['SESS_PRODUCT_CODE']; global $columnlist; $columns_query= "Select column_name from columns_list_to_display where brand_id=$brand_id"; $result=mysql_query($columns_query); $row_num=mysql_num_rows($result); $i=0; while ($row = mysql_fetch_array($result)) { $columnlist = ""; $i++; if($i != $row_num) { echo $row['column_name'].", "; } else { echo $row['column_name'].""; } } } I am writting query like as below: Code: [Select] $query = "SELECT " . $columnlist . "FROM $userlead_table LEFT JOIN notes ON $userlead_table.id = notes.leads_id LEFT JOIN lead_status ON $userlead_table.status = lead_status.status_code WHERE ((Date(a.submitted)>='$datefrom' AND Date(a.submitted)<='$dateto')) ORDER BY a.submitted DESC"; Please let me know where i am doing wrong. Hope one of this forum member would have proper solution for it. Thanks in advance. Regards, Jitendra Question 1) Is the only and proper way to call a parent function "parent::function()"? Are there other/better ways from within a child function? Question 2) What are the deciding factors for when to make a function or attribute static? How do you make that decision? Assuming 5.3... Thanks. I need to call usort from a class function, and I'm puzzled about how to define the comparison function. I've tried to define the comparison function in the same class, but I can't get usort to call it. I found one hint that it will work if I make the comparison function static, but I tried that, and it didn't work for me. If I define the comparison function outside the class, it won't have access to object properties that it needs to operate. The only solution I can think of is to define the comparison function outside the class and put the object properties it needs in globals. Is there a cleaner way to do this? Hi. I'm a PHP newbie. I'm trying to generate a form in a loop. The user would press the submit button and the loop would iterate. Ultimately, it's for a project that will read a large flat file and get 500 lines at a time that the user could page through. But, my little test case is far simpler. just increment a counter every time the user presses submit. What I've tried doesn't work; causes an endless loop rather than stopping each time for the user to hit submit. I've just cobbled this together from things I've seen here and elsewhere, so please be gentle. Any help would be greatly appreciated. Thanks, Rick <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Loop Test</title> </head> <body> <?php $i = 0; while ($i < 5) { echo '<form name="PHP" action="'.$PHP_SELF.'" method="POST">'; echo '<input type="submit" name="click_php" value="PHP form" />'; echo '</form>'; if($_POST['click_php']) { echo "This is from PHP form ==> $i"; $i++; } } ?> </body> </html> Hi, I got a little problem with calling a PHP function from a href. I got my site all set up, and am calling the function like this: Code: [Select] <?php echo '<a href="'.changeRecentPosts().'" class="image">' ?> This does indeed work, but it calls the function every time I refresh my page.. Which isn't the effect I was trying to achieve. I tried to change it to : Code: [Select] <?php echo '<a href="#" onClick="'.changeRecentPosts().'" class="image">' ?> But that didn't work either. It also seems it only calls it when I refresh my page, but never when I click it (I guess because PHP is handeled before HTML or something similar?). If you can think of any way (even in AJAX, or whatever other language...) feel free to, I think I can figure it all out. Thank you! I am using simple machines forum, and by placing a link to ssi.php in the top of my php page Code: [Select] <?php require_once('/forum/SSI.php');?>I am able to use some ssi functions. My problem is calling another function from within a function. My first function is this: Code: [Select] <?php $allowed_groups = array(1,5); $can_see = FALSE; foreach ($allowed_groups as $allowed) if (in_array($allowed, $user_info['groups'])) { $can_see = TRUE; break; } if ($can_see) { echo ' <div> this is text that i want certain groups to see </div>'; } else { //message or section to show if they are not allowed. echo 'this is text that i dont want certain groups to see'; } ?> This is my second function: Code: [Select] <?php ssi_welcome(); ?> I want to put the second function inside the first function, like so: Code: [Select] if ($can_see) { echo ' <div> <?php ssi_welcome(); ?> </div>'; this just gives me the "<?php ssi_welcome(); ?>" displayed on the actual site, it is not calling the separate function. I have tried different approaches, e.g. $function ssi_welcome; but all it does is display this as text as well.. Please can some one point me in the right direction. Thank you When ever I try to google this I always get it the other way round "Calling PHP from jQuery" which is now what I need. Basically I want, When the script starts, For PHP to check if an array is empty and if it's not call a jQuery function (This would be the jQuery UI Dialog box). If you can help me here or even post a link to a resource then I will kiss ya. No homo. Thanks in advance! Whenever I want to use AJAX (using jQuery) I store the PHP code in a seperate file and then send an retrieve data from that file using AJAX. However I was wondering if you can just call a PHP function. Then it would save me having to put each function in a seperate file? Thanks for any help. Hi, I was looking around the web for a image resizer script and I came across this one. http://www.scriptol.com/scripts/thumbnail-maker.php The weird thing is, it says to use this syntax to get it to work php resizer.php -j myimage.png Well.. I don't seem to understand how that makes it work, Normally I would run a php script by looking for it's main function and running it like: mainfunction(); Maybe I'm interpreting it wrong, but how is this script suppose to run? I have this piece of code: Code: [Select] function newmessage() { box = new LightFace({ title: 'New message', width: 600, height: 400, content: "<form method='post' action='sendmessage.php'>To: <br><input type='text' name='to'><br><br>Subject: <input type='text' size='94' name='subject'><br><br><textarea name='message' rows='13' cols='63'></textarea><br><br><input type='submit' value='Send' name='send'><input name='from' type='hidden' value='<? echo $username; ?>'></form>", buttons: [ { title: 'Close', event: function() { this.close(); } } ] }); box.open(); } As well as this piece of code: Code: [Select] $userid = mysql_real_escape_string($_SESSION['userid']); //WALLNOT $db2 = "SELECT status FROM comments WHERE touser='$username' AND status='0'"; $db2connect = $database->query($db2); $status2 = "SELECT status FROM friends2 WHERE user2='$username' and status='0'"; $test = $database->query($status2); $statfromdb = "SELECT stat FROM wallposts WHERE person='$username' AND stat='0'"; $status = $database->query($statfromdb); $GetStatus = "SELECT byuser, status, dtime FROM commentstatus WHERE status='$Naw'and person='$username' AND byuser!='$username'"; $ConnectGetStatus = $database->query($GetStatus); $Getmsg = "SELECT status FROM messages WHERE status='0' AND touser='$username'"; $ConnectGetmsg = $database->query($Getmsg); $row = mysql_fetch_array($test); $row2 = mysql_fetch_array($ConnectGetmsg); $getrow = mysql_fetch_array($status); $getrow2 = mysql_fetch_array($db2connect); $StatusComment = mysql_fetch_array($ConnectGetStatus); //Select profile picture $GetProfilePicture = "SELECT profilepicture FROM users WHERE username='$username'"; $ConnectProfilePicture = $database->query($GetProfilePicture); $ProfilePicture = mysql_fetch_array($ConnectProfilePicture); $MyProfilePicture = $ProfilePicture['profilepicture']; That I use in 19 different pages, is there anyway I can put them all into one page, and just them through a function? It takes so much time when loading if I have it on each page? And if you have any other tips how to optimize my loading speed please write them. Thank you in advance! *Edit: Bad grammar. Hi all! First off, sorry if this question is a completely dumb one but i'm really new to PHP. I'll paste the code to start with... function getReasons ($id) { global $link; $rst = mysql_query('SELECT r.name as reason FROM entries_feedback_comments as f LEFT JOIN reasons as r ON r.id = f.reason_id WHERE f.feedback_id ='. $id, $link); while ($row = mysql_fetch_assoc($rst)) { echo $row['reason']; } } later on in the code.... inside a table structure i do this... echo '<tr class="' . ($y?'even':'odd') . '">'; echo '<td colspan="10" class="freetext">'.getReasons($row['id']).'</td>'; echo '</tr>'; echo '<tr class="' . ($y?'even':'odd') . '">'; echo '<td colspan="10" class="freetext">'.$row["freetype"].'</td>'; echo '</tr>'; (I have left put the "freetype" bit of code in to show you that it works with this row but not with the function). Basically, what I see is the result of the function outside of the table itself rather than in the table row. Is there a reason for this? Does a function have to be put in specific places in the code? Thank you for your help. Sam Hi guys, I am having a tough time executing the code inside the javascript. Everything is working fine with the Php code and I am able to see the output of variable myValue too.. But the problem is nothing is being executed inside the javascript..Not even the first statement of the javascript..When I look at the output sent by the HTML to the browser, the output is what is listed after this code which is right at the bottom of this page. Please advise. Code: [Select] <html> <head> <title>AES (Rijndael) Encryption Test in JavaScript</title> <script src="aes-enc.js" type="text/javascript" language="JavaScript"></script> <script src="aes-dec.js" type="text/javascript" language="JavaScript"></script> <script src="aes-test.js" type="text/javascript" language="JavaScript"></script> <script type="text/javascript"> function doDecryption() { document.println("Inside Javascript"); var ct, key; var theForm = document.forms[0]; blockSizeInBits=128; keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value; if (theForm.key.value.toLowerCase().indexOf("0x") == 0) theForm.key.value = theForm.key.value.substring(2); if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0) theForm.ciphertext.value = theForm.ciphertext.value.substring(2); if (theForm.key.value.length*4 != keySizeInBits) { alert("For a " + keySizeInBits + " bit key, the hex string needs to be " + (keySizeInBits / 4) + " hex characters long."); if (theForm.key.select) theForm.key.select(); return; } if (theForm.ciphertext.value.length*4 != blockSizeInBits) { alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " + (blockSizeInBits / 4) + " hex characters long."); if (theForm.ciphertext.select) theForm.ciphertext.select(); return; } ct = hex2s(<?php echo $myValue; ?>); document.println("Inside Javascript"); document.write(ct); // key = hex2s(theForm.key.value); // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB")); } </script> </head> [syntax=php] <?php mysql_connect("localhost","root",""); mysql_select_db("encryption") or die(mysql_error()); $userId = $_POST['userId']; if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'] == "")) { $query = mysql_query("select * from employee_details where id = '$userId'"); if($row=mysql_fetch_assoc($query)) { echo '<tr>'; foreach($row as $value) echo '<td>'.$value.'</td>'; echo '</tr>'; } else { echo "No rows returned"; }} else if (($_SERVER['REQUEST_METHOD'] == 'POST') && ($_POST['key'])) { $columname = "ciphertext"; $tablename = "employee_details"; function getField($field, $tbl_name, $condition) { $result = mysql_query("SELECT $field FROM $tbl_name WHERE id = ".$condition); return @mysql_result($result, 0); } $myValue = getField($columname,$tablename,$userId); echo "$myValue"; //doDecryption(); } echo '<script type="text/javascript"> doDecryption(); </script>'; echo "whats happening"; ?> </body> </html> [/syntax] <body> CODE THAT IS SENT AS HTML OUTPUT TO THE BROWSWER Code: [Select] <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>AES (Rijndael) Encryption Test in JavaScript</title> <script src="aes-enc.js" type="text/javascript" language="JavaScript"></script> <script src="aes-dec.js" type="text/javascript" language="JavaScript"></script> <script src="aes-test.js" type="text/javascript" language="JavaScript"></script> <script type="text/javascript" language="JavaScript"> function doDecryption() { document.alert("Inside Javascript"); var ct, key; var theForm = document.forms[0]; blockSizeInBits=128; keySizeInBits = theForm.keySize[theForm.keySize.selectedIndex].value; if (theForm.key.value.toLowerCase().indexOf("0x") == 0) theForm.key.value = theForm.key.value.substring(2); if (theForm.ciphertext.value.toLowerCase().indexOf("0x") == 0) theForm.ciphertext.value = theForm.ciphertext.value.substring(2); if (theForm.key.value.length*4 != keySizeInBits) { alert("For a " + keySizeInBits + " bit key, the hex string needs to be " + (keySizeInBits / 4) + " hex characters long."); if (theForm.key.select) theForm.key.select(); return; } if (theForm.ciphertext.value.length*4 != blockSizeInBits) { alert("For a " + blockSizeInBits + " bit block, the hex ciphertext string needs to be " + (blockSizeInBits / 4) + " hex characters long."); if (theForm.ciphertext.select) theForm.ciphertext.select(); return; } ct = hex2s(<br /> <b>Notice</b>: Undefined variable: myValue in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>41</b><br /> ); document.write("Inside Javascript"); document.write(ct); // key = hex2s(theForm.key.value); // theForm.plaintext.value = byteArrayToHex(rijndaelDecrypt(ct, key, "ECB")); } </script> </head> <body> <br /> <b>Notice</b>: Undefined index: userId in <b>C:\wamp\www\AES4U\Decryptedresults.php</b> on line <b>58</b><br /> // Do not worry about this notice because teh value has not been posted it is not able find this value. </body> Code: [Select] <?php class My { public function disp() { echo "ehllo"; } } ?> <html> <head> <script type="text/javascript"> function displaymessage() { document.write(My.disp()); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> </body> </html> HEllo I wanted to know wheter php function can be call in js functions. Please Help. can someone help me I am trying to call a class function using a varible.... example say i go to index.php?page=contact i want to pull that class with out doing a switch statement can someone help me Code: [Select] <?php $Site = page; $Site->getPage($_GET['page']); ?> Code: [Select] <?php class page extends site { function getPage($page) { return $this->$page; } function Contact() { echo '<h2>Contact test</h2>'; echo '<form> </form>'; } } ?> This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=316624.0 Trying to run this function so it displays the generated image in a table cell - Code: [Select] <?php function imgsecuregen($size = 6){ $width = 11*$size; $height = 25; $string = ""; for($i = 1; $i <= $size; $i++){ $string .= rand (0,9).""; } // for $im = ImageCreate($width, $height); $bg = imagecolorallocate($im, 102, 102, 102); // background $black = imagecolorallocate($im, 0, 255, 0); // text $grey = imagecolorallocate($im, 102, 102, 102); // border imagerectangle($im,0, 0, $width-1, $height-1, $grey); imagestring($im, 5, $size, 5, $string, $black); imagepng($im); imagedestroy($im); } imgsecuregen(8); // string length ?> Works if I load it directly, but not in a <td>. OK, in my html file, I have "${uri}" which is a directory/filename. A link (href) is created directly from "${uri}" that opens the file when clicked. This works great. <dd><a href="${uri}">${uri}</a> (${size} bytes)<br><br></dd> Now, I want to also add the ability to explor the directoy. So I want to add another link (href) with the directory only include in "${uri}". For example, if "${uri}" = '/this/is/the/directory/and/thefile.txt', I want an href w/ '/this/is/the/directory/and' I created the file dir.php w/ the following <?php function giveme_dir_only( $a ) { $mydir = dirname($a ); return $mydir; } ?> Now, how do I add this to my html/href command. There is no reason to run the dir.php until the user actually clicks on the link, eh? <dd><a href="xxxx">"xxxx"</a> <br><br></dd> Thanks in advance for the help! |