|
gwar3k1 posted:Ah okay, sorry. If you look at my previous example, you can access each array item individually by using $row['fieldname']. Try the following: php:<? // loop all returned rows while($row = mysql_fetch_assoc($query) { // loop all items within the $row array $elements = count($row) - 1; for(i = 0; i<=$elements; i++) { // conditional select based on index of $row array switch($i) { // first two indexes case 0: case 1: // output non-field cell break; // all other indexes default: // output field cell break; } } }?> DoctorScurvy fucked around with this message at 08:18 on Apr 26, 2010 |
# ? Apr 25, 2010 22:40 |
|
|
# ? Jun 6, 2024 05:46 |
|
I've been using the Shopp plugin for Wordpress, and my boss wanted me to make each products "Details and Specs" be a tabbed interface. The box for details and specs is unusably small, so I wrote a php script to take the info and split it into the 3 tabs he wants. For some reason, it seems like PHP is adding backslashes to the code in certain places (they seem to be in front of quotation marks mostly.) Why is it doing this?php:<? <center><h2>Product Tab-ulator</h2></center> <form action="formatter.php" method="post"> <div> Product Info: (Product Details Tab)<br> <textarea name="item_longdesc" rows="5" cols="80"><?php echo $_POST[item_longdesc] ?></textarea><br> </div> <div style="float: left;"> Specification name: (Specifications Tab, Left Column)<br> <textarea name="item_desc" rows="30" cols="40"><?php echo $_POST[item_desc] ?></textarea><br> </div> <div style="float: left;"> Specification value:(Specifications Tab, Right Column)<br> <textarea name="item_spec" rows="30" cols="70"><?php echo $_POST[item_spec] ?></textarea><br> </div> <div> Updates Available:(Updates tab)<br> <textarea name="item_updates" rows="30" cols="40"><?php echo $_POST[item_updates] ?></textarea><br> <input type="submit" /> </div> </form> <?php $header="<div class='tabber'><div class='tabbertab'><h2>Product Details</h2><p>"; $header=$header. $_POST[item_longdesc] . "</p></div><div class='tabbertab'><h2>Product Specifications</h2>"; $header=$header. "<p><div style='min-height:500px;' ><div class='itemlist'>"; //<div class='itemdesc'>40<br></div></p></div></div> $idesc=explode("\n", $_POST[item_desc]); $ispec=explode("\n", $_POST[item_spec]); foreach ($idesc as $item){ $header = $header . "<b>". $item . "</b><br>"; } $header=$header . "</div> <div class='itemdesc'>"; foreach ($ispec as $item){ $header = $header . $item . "<br>"; } $header=$header . "</div></div></div><div class='tabbertab'><h2>Product Updates</h2><p>" . $_POST[item_updates] . "</p></div></div>"; echo "<h2>Results - Do not use until you have hit the submit query button:<br><div style='background-color: #C4E5F3;'>"; echo htmlspecialchars($header) . "</div>"; ?> ?> the chip fucked around with this message at 01:32 on Apr 26, 2010 |
# ? Apr 25, 2010 23:59 |
|
Dargor posted:Hey Im sorry that this post breaks the tables. I went back to try to fix this, but the PHP code I posted actually ran on the preview page. Can either a mod fix this or someone tell me what to do to unbreak the tables with my post You have to something like this: php:<? $header = "<div class='tabber'><div class='tabbertab'><h2>Product Details</h2><p>"; $header .= $_POST[item_longdesc] $header .= "</p></div><div class='tabbertab'><h2>Product Specifications</h2><p>"; $header .= "<div style='min-height:500px;' ><div class='itemlist'>"; ?>
|
# ? Apr 26, 2010 01:12 |
|
Thanks Lumpy. EDIT: duh, stripslashes() the chip fucked around with this message at 04:36 on Apr 26, 2010 |
# ? Apr 26, 2010 01:31 |
|
DoctorScurvy posted:Use default: instead of case else: and remember that each case needs to end with break; or it will run the code of every case after it. Also, just as an efficiency thing, it's better to set $elements = count($row) - 1 then use $elements in the loop because that way it only needs to run the count() function once. Yeah I didn't check the case else/default thing, I've been switching between languages all week and got confused. Good point about the count() usage. I am curious though, I was showing how to fall through cases so that case 0 and case 1 run the same code (// output non-field cell). By including breaks isn't this preventing that feature, like you said or is it a case of them being in-line doesn't affect the fall through? I had the break for both of them below where the code (commented for simplicity) lies.
|
# ? Apr 26, 2010 08:11 |
|
gwar3k1 posted:I am curious though, I was showing how to fall through cases so that case 0 and case 1 run the same code (// output non-field cell). By including breaks isn't this preventing that feature, like you said or is it a case of them being in-line doesn't affect the fall through? I had the break for both of them below where the code (commented for simplicity) lies.
|
# ? Apr 26, 2010 08:15 |
|
I have a script that reads a file based on the query, and gets a random line from the file:php:<? $filename = $_GET['q']; if(preg_match('/^[a-z0-9]+$/i', $filename)) { $items = array_map("trim", file($filename.".txt")); $randomitem = $items[array_rand($items)]; } else { $randomitem = ''; } ?> Is there a better way to do something like this? I'm aware of the issues about someone putting in "/.." or similar for the query to view files out of the current directory, thus I limited the query to alphanumerics. I don't mind if they enter in a filename and come across a different file, just that they shouldn't view anything outside of the current directory.
|
# ? Apr 26, 2010 13:50 |
|
As long as you're sanitizing the input file name it should be okay, although I would add a base path to the $filename and also check if the file exists too.
|
# ? Apr 26, 2010 15:35 |
|
I got my search feature (that I was trying to do before with WP) working near perfect, but this kills it:code:
code:
|
# ? Apr 26, 2010 19:19 |
|
Lumpy posted:You have to something like this: Though in all fairness this is loving awful and I cant think of a reason to have something like this in well templated PHP.
|
# ? Apr 26, 2010 19:51 |
|
drcru posted:True, how do you guys do it? I've never tried before. I whipped up a simple templating class a while back, this is it slightly modified: php:<? /** * Simple templating engine. */ class TemplateRenderer { /** * @var array Key/Value pair array of variables. */ protected $_variables; /** * Initialise. */ public function __construct() { $this->_variables = array(); } /** * Magic getter for getting template vars. * @param string $key Name of the var to attempt to get. * @return mixed Variable requested or null if not found. */ public function __get($key) { if (isset($this->_variables[$key])) { return $this->_variables[$key]; } return null; } /** * Set a template variable. * @param string $key Variable key. * @param mixed $value Variable value. */ public function set($key, $value) { $this->_variables[$key] = $value; } /** * Extract template variables and render a template. * @param string $template Location of template to render. */ public function render($template) { if (is_readable($template)) { include($template); return true; } return false; } ?> code:
php:<? $t = new TemplateRenderer(); $t->set('some_var', 'Hello World!'); $t->render('/path/to/template.php'); ?>
|
# ? Apr 26, 2010 21:29 |
|
ad homonym posted:I have a script that reads a file based on the query, and gets a random line from the file: php:<? $validtxt = array('stuff1', 'stuff2', 'stuff3'); $filename = $_GET['q']; $randomitem = ''; if (in_array($filename, $validtxt)) { $items = file($filename.'.txt') or die("File not found, please inform admin"); $randomitem = trim($items[array_rand($items)]); } else { die("Incorrect filename provided"); } ?>
|
# ? Apr 26, 2010 22:03 |
|
haywire posted:Though in all fairness this is loving awful and I cant think of a reason to have something like this in well templated PHP. Indeed, but it doesn't break the forum tables
|
# ? Apr 27, 2010 00:18 |
|
Pookster posted:Using PHP in the templates is still quite verbose but it's only meant as a quick and simple solution. php:<? $_ = array('some_var' => 'Hello World!'); include '../templates/my-template.php'; ?> code:
MrMoo fucked around with this message at 10:40 on Apr 27, 2010 |
# ? Apr 27, 2010 03:12 |
|
MrMoo posted:and the template,
|
# ? Apr 27, 2010 10:28 |
|
Probably, mixing up gettext
|
# ? Apr 27, 2010 10:39 |
|
<? ?> (short tags) are not recommended because not all servers support them. This is a configurable option in php.ini and for whatever reason, some servers have this disabled. Also short tags are deprecated in PHP6, so it might be best practice to start coding without using short tags.
|
# ? Apr 27, 2010 14:12 |
|
DarkLotus posted:<? ?> (short tags) are not recommended because not all servers support them. This is a configurable option in php.ini and for whatever reason, some servers have this disabled. Also short tags are deprecated in PHP6, so it might be best practice to start coding without using short tags. I haven't looked into templating, but if short tags are removed, how do you output dynamic content without also producing the html on the fly?
|
# ? Apr 27, 2010 17:21 |
|
gwar3k1 posted:I haven't looked into templating, but if short tags are removed, how do you output dynamic content without also producing the html on the fly? This: <?php echo $variable; ?> Is the same as: <?=$variable?>
|
# ? Apr 27, 2010 17:29 |
|
gwar3k1 posted:I haven't looked into templating, but if short tags are removed, how do you output dynamic content without also producing the html on the fly? immediate answer: <?echo $variable?> and <?= $variable?> are the same when short tags (i.e. <?= ?>) are permissible short answer: i output dynamic content by using mature libraries that actually do important stuff like sanitization long answer: holy poo poo, are you serious? is the state of online php tutorials still that loving terrible, or did you stop reading them in 1998?
|
# ? Apr 28, 2010 02:23 |
|
Oh right, so its a case of inline tags are still OK but they should be explicitly doing something not loosely doing it? I didn't know you could use =$var. And yeah, I stopped reading tutorials when I got the basics of PHP+MySQL down, only using the manual for reference. I've come across templating in the past (mostly questions posed here) and its looked pretty complex for my requirements, but yeah, I should take a look.
|
# ? Apr 28, 2010 17:19 |
|
gwar3k1 posted:Oh right, so its a case of inline tags are still OK but they should be explicitly doing something not loosely doing it? I didn't know you could use =$var. No, it's a case of quick tags <?= $poop; ?> not being a good practice because A) they are a server by server configuration option and B) they are going away.
|
# ? Apr 28, 2010 17:49 |
|
It's a fine practice if you're staying with PHP4 because you prefer the stability of older releases
|
# ? Apr 28, 2010 17:55 |
|
I have a main class (Foo) with a member which is set to the name of another class (SingleStatic) which only has one static function (functionA). I want to call that static function from within the bar fucntioncode:
|
# ? Apr 29, 2010 06:28 |
|
Harry Krishna posted:I have a main class (Foo) with a member which is set to the name of another class (SingleStatic) which only has one static function (functionA). I want to call that static function from within the bar fucntion What's wrong with: php:<? class Sta{ static public function funcA() { echo "OOOOH!"; } } class Stb{ public function bar() { Sta::funcA(); } } $pie = new Stb(); $pie->bar(); ?> php:<? class Sta{ static public function funcA() { echo "OOOOH!"; } } class Stb{ private $className; private $methodName = 'funcA'; function __construct( $n ) { $this->className = $n; } public function bar() { //call_user_func( $this->className . '::' . $this->methodName ); // works in 5.3.x and up call_user_func( array( $this->className , $this->methodName ) ); } } $pie = new Stb( 'Sta' ); $pie->bar(); ?> Lumpy fucked around with this message at 15:37 on Apr 29, 2010 |
# ? Apr 29, 2010 15:18 |
|
Ok, we're slogging through our big project thing...The idea is that the user can get usage statistics for the campus online course platform. And by usage statistics, we mean SELECT COUNT(*) type statistics...basically the number of hits in our log database. So the user (using drop down boxes) specifies the semester, and then a college, department, and course followed by a "tool view"...In this example, this is all defined at the bottom via the $userselected(blank) variable. If you notice, all that is selected is term and tool...we're trying to craft the code for different queries based on the scenario. Say that didn't specify a college/department/course...that means the user just wants overall stats for a tool's usage in a semester. OK, now onto the problem(s)...we've got this set to loop through some arrays. Look at $counter...it always returns "0" when we echo it. Same for the following department and college counters... Any ideas as to what we're doing wrong? EDIT: I chopped down the "userselected" var names because I'm kind of breaking tables...if you see any discrepancies in the $user variables, it's not like that in our regular code EDIT2: Also, disregard the print_r stuff...just trying to see what's going on behind the scenes EDIT3: First half of code removed, see post towards bottom of page for full code hasegawa fucked around with this message at 20:08 on Apr 30, 2010 |
# ? Apr 29, 2010 19:23 |
|
At a guess, I'd say its always 0 because collegeCount() always returns in the first iteration of the while loop?
|
# ? Apr 29, 2010 20:41 |
|
Yay posted:At a guess, I'd say its always 0 because collegeCount() always returns in the first iteration of the while loop? Surely it'd return the last? - It'll re-write $counter every single time, because it's not being appended, it's being declared. There's probably more issues in there, but just from what you said, surely something like this would solve it? php:<?php $counter = 0; while($allCollegeArray = mysql_fetch_array($allCollegeQuery, MYSQL_ASSOC)) { $add = collegeCount($allCollegeArray['college_id'], $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool); $counter+$add; } echo $counter; ?> (Not saying any of that code will actually work, but pseudo-wise, that'd solve that part of the issue?) SiCk fucked around with this message at 11:04 on Apr 30, 2010 |
# ? Apr 30, 2010 10:58 |
|
hmm...I just tried those recommendations (moving the $counter variable before the while statement, echoing it after the while statement) but it's still just returning a list of zeros alongside the list of increasing values from the array. So that means that the looping statement is working, but the counter isn't. And the SQL works, I can run it in PHPmyadmin and get plenty of hits as a result...not sure what's going on. Still, thanks for the help thus far. EDIT: If you set $counter=2, and then echo $counter; echo "<br />"; echo $add; You'll get 02 0 As output. The array stuff comes form the dept loop, but I'm guessing "02" is $counter and "0" is $add. I guess that means that it isn't calling the collegeCount() function correctly? hasegawa fucked around with this message at 16:21 on Apr 30, 2010 |
# ? Apr 30, 2010 13:36 |
|
Is there a pre-made php script that, given a mysql table, will show it in an access-style table, where it picks up on the fields names and allows you to sort and filter, like you could do in an access table view or excel spreadsheet?
|
# ? Apr 30, 2010 15:12 |
|
What you're looking for is called a "scaffolding".
|
# ? Apr 30, 2010 16:52 |
|
hasegawa posted:hmm...I just tried those recommendations (moving the $counter variable before the while statement, echoing it after the while statement) but it's still just returning a list of zeros alongside the list of increasing values from the array. So that means that the looping statement is working, but the counter isn't. Just noticed you have the same issue in your while statement within the collegecounter() as i pointed out in your first function.. ( again i'm not great with php, so bear with me if this is wrong.. i'm sure someone will correct me if i'm being a tard ) try this? - I'm not wholly sure you can add variable to variable like '$one+$four', but i dont see why you couldnt, providing they're both integers. php:<?php $collegeCounter = 0; while($collegeArray = mysql_fetch_array($collegeQuery, MYSQL_ASSOC)) { echo "<br />\n"; print_r($collegeArray); echo "<br />\n"; $toolSQL = "SELECT tool_identifier FROM tooltable WHERE tooltable.tool_id = '" . $userSelectedTool . "'"; $ToolString = mysql_query($toolSQL); $deptCounter += deptCount2($usrSelClg, $collegeArray['dept_id'], $usrSelCourse, $usrSelTerm, $usrSelTool); echo $deptCounter; $collegeCounter += $deptCounter; } return $collegeCounter; ?> edit: looked it up, there is no reason why you cant do $this+that, but it's supposed to be $this += $that.. but you already knew that, you had it already. I've learned something today! hoorah! SiCk fucked around with this message at 17:00 on Apr 30, 2010 |
# ? Apr 30, 2010 16:56 |
|
SiCk posted:You were returning $collegeCounter every time that while was run, you need to append to it, and not return it a few times. This is all dependant on whether or not that 'deptCount2()' function is working too.. This is mainly for the guy who started all this, but a function can't "return multiple times" Once you hit a return, your function stops executing. php:<? function poop() { for( $i = 0; $i < 100000; $i++ ) { echo "$i"; return $i; } } function blah() { while( TRUE ) { echo "lol"; return 'snacks'; } } poop(); blah(); ?> hasegawa, it seems that the function you don't show us is probably doing something equally as bad as the code you have shown us, so I'd look there.
|
# ? Apr 30, 2010 17:04 |
|
That makes sense.. cheers, good to know
|
# ? Apr 30, 2010 17:53 |
|
Lumpy posted:hasegawa, it seems that the function you don't show us is probably doing something equally as bad as the code you have shown us, so I'd look there. Ok, here's the entire thing..I'll edit the code out of my first post so that you don't have to scroll twice as long to get here. PS: Sorry for more table breakage php:<?php $con = mysql_connect("localhost", "name", "pass"); if(!$con) {die("could not connect to MYSQL");} mysql_select_db("db1", $con); function selectProperArrayType($usrSelClg, $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool) { if ($usrSelClg == "" && $usrSelDept == "" && $usrSelCourse == "") { $allCollegeSQL = "SELECT college_id, name FROM collegetable ORDER BY name"; $allCollegeQuery = mysql_query($allCollegeSQL); $counter = 2; while($allCollegeArray = mysql_fetch_array($allCollegeQuery, MYSQL_ASSOC)) { $add = collegeCount($allCollegeArray['college_id'], $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool); $counter+=$add; } echo $counter; echo "<br />"; echo $add; } else { echo "problem"; } } function collegeCount($usrSelClg, $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool) { $collegeSQL = "SELECT dept_id, dept_name FROM db1.depttable LEFT JOIN db1.collegetable ON db1.depttable.college = db1.collegetable.college_id WHERE college_id = '" . $usrSelClg . "' ORDER BY dept_name ASC"; echo $collegeSQL; echo "<br />\n"; $collegeQuery = mysql_query($collegeSQL); while($collegeArray = mysql_fetch_array($collegeQuery, MYSQL_ASSOC)) { echo "<br />\n"; //print_r($collegeArray); echo "<br />\n"; $toolSQL = "SELECT tool_identifier FROM db1.db1_tools WHERE db1.db1_tools.tool_id = '" . $usrSelTool . "'"; $ToolString = mysql_query($toolSQL); $deptCounter += deptCount2($usrSelClg, $collegeArray['dept_id'], $usrSelCourse, $usrSelTerm, $usrSelTool); echo $deptCounter; //$collegeCounter = $deptCounter; //return $collegeCounter; $collegeCounter += $deptCounter; } return $collegeCounter; } function deptCount2($usrSelClg, $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool) { $deptSQL = "SELECT id, longtitle FROM proddb.coursetble LEFT JOIN db1.depttable ON db1.depttable.dept_name = proddb.coursetble.depttitle WHERE db1.depttable.dept_id = '" . $usrSelDept . "' AND proddb.coursetble.term = '" . $usrSelTerm . "' ORDER BY longtitle DESC "; $deptQuery = mysql_query($deptSQL); while($deptArray = mysql_fetch_array($deptQuery, MYSQL_ASSOC)) { echo "<br />\n"; print_r($deptArray); echo "<br />\n"; $toolSQL = "SELECT tool_identifier FROM db1.db1_tools WHERE db1.db1_tools.tool_id = '" . $usrSelTool . "'"; $ToolString = mysql_query($toolSQL); $usageQuantity = getQuantity($deptArray['id'], $ToolString); return $usageQuantity; } } function getQuantity($usrSelCourse, $ToolString) { $ToolArray = mysql_fetch_array($ToolString, MYSQL_ASSOC); $sql = "SELECT count(*) as Quantity FROM db1.apache_log LEFT JOIN proddb.courseadmin ON proddb.courseadmin.id = db1.apache_log.log_id_param LEFT JOIN proddb.enroll_table ON proddb.enroll_table.coursesourcedid = proddb.courseadmin.idnumber LEFT JOIN proddb.people ON proddb.people.username = db1.apache_log.log_user LEFT JOIN proddb.coursetble ON proddb.coursetble.sourcedid = proddb.courseadmin.idnumber WHERE db1.apache_log.log_request = '" . $ToolArray['tool_identifier'] . "' AND proddb.enroll_table.personsourcedid = proddb.people.sourcedid AND proddb.enroll_table.role = 1 AND proddb.coursetble.id = '" . $usrSelCourse ."'"; $result = mysql_query($sql); $row = mysql_fetch_array($result, MYSQL_ASSOC); return $row['Quantity']; } ?> <!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>Untitled Document</title> </head> <body> <?php $usrSelTerm = "201010"; $usrSelClg = ""; $usrSelDept = ""; $usrSelCourse = ""; $usrSelTool = "6"; selectProperArrayType($usrSelClg, $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool); ?> </body> </html> hasegawa fucked around with this message at 20:50 on Apr 30, 2010 |
# ? Apr 30, 2010 20:07 |
|
hasegawa posted:Ok, here's the entire thing..I'll edit the code out of my first post so that you don't have to scroll twice as long to get here. php:<? function deptCount2($usrSelClg, $usrSelDept, $usrSelCourse, $usrSelTerm, $usrSelTool) { // STUFF SNIPPED while($deptArray = mysql_fetch_array($deptQuery, MYSQL_ASSOC)) { echo "<br />\n"; print_r($deptArray); echo "<br />\n"; $toolSQL = "SELECT tool_identifier ... "; $ToolString = mysql_query($toolSQL); $usageQuantity = getQuantity($deptArray['id'], $ToolString); //////////// RETURN IN A WHILE LOOP ///////////// return $usageQuantity; ////////////////////////////////////////////////// } } ?>
|
# ? Apr 30, 2010 20:52 |
|
Lumpy posted:
quote:Array ( [dept_id] => 2 [dept_name] => Anthropology ) Time out...is it some sort of infinite while loop we've invoked? EDIT: Ok...my group member just revealed to me that the code I've been using is "Results2". He created it because results1 wouldn't do the colleges properly. Upon playing with results1 (and pulling the "return"s out of the while loops), it sort of works. quote:Accounting: 0 I created 3 people for a class in each department of that college....1 teacher and 2 students per class. Since the code specifies that the role=student, that's why we're seeing 2. So it (sort of) works. And if I set a department, it will list out all of the courses within that department and the class that I created students for will also say 2. That's good. But, when we try to do it for all colleges, it does this: quote:0 EDIT2: Ok...he's getting better results by using global variables. I think we are kinda making progress so for now I'll say "Thanks for the help it (kinda) got us back on track". hasegawa fucked around with this message at 21:30 on Apr 30, 2010 |
# ? Apr 30, 2010 21:06 |
|
Pissflaps posted:Is there a pre-made php script that, given a mysql table, will show it in an access-style table, where it picks up on the fields names and allows you to sort and filter, like you could do in an access table view or excel spreadsheet? phpMyAdmin
|
# ? Apr 30, 2010 21:14 |
|
hasegawa posted:Ok, I just bumped "return $usageQuantity;" to the next curly brace (outside of the while loop)...and this time, it did this: The web server has a limit on how long a PHP script can execute so scripts that *do* run infinite loops can't hog all the resources of the server. Your server's time limit per script appears to be 60 seconds, and your script is taking longer than that to run.
|
# ? Apr 30, 2010 21:24 |
|
|
# ? Jun 6, 2024 05:46 |
|
Ok, so I got this wordpress blog theme (simplr) that I'm trying to customize a little. Right now it's got this poo poo at the bottom: and I want to get rid of everything there except for the archives box. The .php file that contains this section is sidebar.php, and is here: http://pastebin.com/wsB1hzct I tried my own cack-handed editing and it just resulted in a php error. I'm stupid and unsure of what to delete and what to leave. Please help.
|
# ? Apr 30, 2010 23:32 |