Jump to content

kicken

Gurus
  • Posts

    4,695
  • Joined

  • Last visited

  • Days Won

    177

Everything posted by kicken

  1. Correction, it was just as broken on the old one. You just had your error reporting level set to not show notices. Array keys which are strings have to be quoted: $course=$_GET['course']; echo $course;
  2. There is no such function mysql_database. As such your script likely is failing with a fatal error to an undefined function. If your not seeing this error in your browser, then your server must be configured to not display errors (display_errors setting in php.ini) or some other configuration issue is preventing them from showing.
  3. You can only return one value. If you want the results as a string, have the function build that string through concatenation and then return it. You could also store each result in an array and return the array. $ret = ''; $query = exec_mysql_query("SELECT * FROM av_genres WHERE id = $cid ORDER BY name DESC"); while ($row = mysql_fetch_assoc($query)) { $ret .= $row['name'].', '; } return $ret;
  4. There are different reasons for caching. One reason is to preserve bandwidth by letting browser store local copies of resources so they do not have to re-request them. This is something the browser manages automatically. Another reason is to cache the results of an expensive operation. For example, resizing a large image into a thumbnail is an expensive operation and can put a lot of stress on a server if it is constantly resizing an image. As such, it is beneficial to do the resize once and save the result in a file. That way for subsequent requests can simple use the cached results rather than having to repeat the resize. You can code your resize script so that it checks for the cached results file and if so, just outputs it. If it does not exist yet, then it can create it.
  5. The terms hostname/servername typically are used to refer to the domain name. The http: part is the protocol or scheme. http://www.example.com/somepath/somefile.php http = Protocol / Scheme. http, https, ftp, etc www.example.com = Host name / Domain name / Server name /somepath/somefile.php = Path I am not aware of any specific term that is commonly used for the combination of the protocol and hostname. If I had to create a variable for it I would probably name it BASE_URL or similar.
  6. Use DomDocument to parse the html into a dom tree, then you can traverse it and find what you need.
  7. I suppose you could call them the URL Root, though I'm not aware of any particular name for them. Generally you don't have to worry about them too much. If you have a link, such as <a href="/index.php">Home<a> And you access the page from say http://local.dev3/somedir/somefile.php then your browser will take that link URL and generate the url http://local.dev3/index.php If you accessed the page from http://www.mysite.com/somdir/somefile.php then your browser would generate the URL http://www.mysite.com/index.php It will automatically use whatever protocol (http vs https) and domain (www.mysite.com vs local.dev3) that you used when you requested the page containing the link. You almost never have to include that information in your links (and probably shouldn't generally) unless you need to force a particular protocol or domain name. Like you might use a full absolute url in a <form action=""> tag to send the user to a https url instead of a http url.
  8. There's no need for your foreach() loop inside your while loop. In fact I would expect it to not work as $row['app_id'] wouldn't be an array. while($row = mysqli_fetch_array($result)) { $checked1[] = $row['app_id']; } That said, can you not just use a JOIN in your first sql query to achieve the desired results? Seems like it should be possible if your just getting ID's and re-querying.
  9. file() leaves the new lines on the end of each line by default. Each entry in your array then is going to have a newline sequence following it which is why your matches do not work. $date_array = file('test.txt', FILE_IGNORE_NEW_LINES);
  10. I do most my dev work in chrome and since they have always had that view source reload problem I got used to using Fiddler2 when I need to debug things like that. Using it you can just find whichever request your interested in and then view it's data. Works well when dealing with xhr requests and such too.
  11. Web Root and Document Root are the same thing generally. They refer to the location from which your website is served by your webserver. Any file/directory below the web root has a corresponding URL from which it can be accessed. Filesystem root is where your filesystem on your hard drive begins. Such as C:\ on windows, or '/' on linux/unix/mac. Script Root could be whatever you want it to be, it doesn't really have any particular meaning. ----- You can define however many "root" directories as you want, just so long as you know what each one is referencing. That is why they are usually prefixed with some other word and not just 'root'. I usually define an includes root, uploads root, templates root, etc: define('DOCUMENT_ROOT', dirname(dirname(__FILE__))); define('TEMPLATE_ROOT', DOCUMENT_ROOT.DIRECTORY_SEPARATOR.'templates'); define('INCLUDE_ROOT', DOCUMENT_ROOT.DIRECTORY_SEPARATOR.'includes'); define('UPLOADS_ROOT', DOCUMENT_ROOT.DIRECTORY_SEPARATOR.'Uploads'); What you just have to know and understand is that regardless of whether you define your own roots or not will have no effect on how a path is resolved unless you use those root constants in your paths. <?php require('/file.php'); ?> The / there will always refer to the filesystem root. If you want to use a different root, you have to prefix the path with the appropriate root constant: <?php require(INCLUDE_ROOT.'/file.php'); ?> Likewise in HTML. <a href="/index.php"></a> Will always refer to the web/document root. If you wanted some other root you would have to echo it out as a prefix.
  12. If you want the constant replaced with it's value, you have to concatenate it with the string, not just embed it. echo '>'.CNT_TXT_ADMIN_CLIENTES.'</a>';
  13. Root on your mac is the '/' directory. Perhaps that is labeled as 'Macintosh HD' in your file viewer. I have zero experience on mac's so I can't really say for sure how it's represented in the GUI. No, root is an absolute term, not relative. In windows, root is the drive letter, eg C:\. Since mac's and linux/unix filesystems do not have drives like windows, root is the '/' directory. In any case, root is always the very top level of the file tree. If you open a shell and run cd / it will take you to your root. You can always setup a constant/variable which points as a specific directory and consider that your app's "Root" but that does not have any effect on what the "/" prefix on a path means, and there is no way to change it. You have to prefix all your paths with the constant/variable you have setup for it to have any effect.
  14. Having a single config file to control site settings like that is still a nice thing to have. It is typically unnecessary for paths but convenient if you have other settings that may change (eg, email settings, DB settings, etc) You need to understand when your dealing with a Web path/URL and when your dealing with a filesystem path. In HTML you are always dealing with a URL path. Your link of /index.php is transformed into http://local.dev3/ (or http://www.MySite.com/ depending on how you've accessed the site). PHP generally always uses a Filesystem path unless otherwise noted in the manual. As such, '/' always references the filesystem root. Whatever you configure your virtual host to has absolutely zero effect on this, php is not restrained by it in any way. It's generally easier to just use relative paths, as they do not generally change unless you go through and restructure your site and move a bunch of files around. You can also auto-detect your root using php's magic constants. I do this generally in my global config file and use it to define a few constants to refer to other directories. Example: define('BASEDIR', dirname(dirname(dirname(__FILE__)))); define('PRIVATEDIR', BASEDIR.DIRECTORY_SEPARATOR.'private'); define('PUBLICDIR', BASEDIR.DIRECTORY_SEPARATOR.'public'); define('ERROR_LOG', PRIVATEDIR.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'errors.log'); That defines BASEDIR to point to where my site is located on the filesystem. eg, given my layout in my previous post, D:\Web\SiteA. Then I define additional constants based on that to determine the location of other directories/files. My code can use these constants when necessary. If you do build your site to use all relative paths, the above is not really necessary. Your main confusion seems to stem from your lack of understanding of when your dealing with a URL vs a Path though. You need to recognize when your dealing with each, and that there is a difference between the two and what is considered to be their 'root'.
  15. No, it should not. Your not in HTML, your in PHP. PHP is based of the filesystem root, not the web root. Your file system root is going to be at least the directory containing 05_Debbie folder, maybe higher if there are more levels. Lets try a full example. I store all my websites that I am working on in my D:\ drive on my laptop in a folder called web. D:\ web\ siteA\ private\ public\ components\ header.php index.php siteB\ private\ public\ moreSites...\ Now, I setup my virtual hosts to use the public folder under each site as the document root. So for example: http://siteA/index.php would correspond do D:\Web\siteA\public\index.php In this setup, our roots would be: Web Root: D:\Web\SiteA\public Filesystem Root: D:\ Inside index.php, we have: <?php require('/components/header.php'); ?> Since PHP uses the filesystem root, that path is going to refer to D:\components\header.php Any HTML paths such as in an image tag are interpreted as a URL, which is always run through the server and is only capable of referencing something under the server's document root. This is why a / there references the web root because the / in HTML is just a shortcut for http://siteA/
  16. Your two sorting functions are completely separate and have no relation to each other. When you call usort($productArray, 'sortElement1Asc'); your sorting the array by the date. Next, when you call usort($productArray, 'sortElement2Asc'); your sorting the array by the price, but in the process you completely overwrite the previous sorting. This second call is a completely separate process with no relation to the previous sort. What you need to do to sort by both is create one single function which will do the sorting you need. First, compare the price. If they differ, return -1 or 1 accordingly. If they are equal, move on to your second condition on the date. Compare the dates and return -1, 0, or 1 as appropriate.
  17. Web Root is not the same as the Filesystem root. The webroot is whatever the webserver is configured to use as their document root, eg $_SERVER['DOCUMENT_ROOT']. The Filesystem root is the root of the filesystem/drive (eg '/' on linux, C:\ (or relevant drive) on windows). PHP paths are based on the Filesystem layout. Since all HTML paths are run through the webserver, they are all based on the Web layout (where the root = document root). When dealing with paths in PHP, you need to know how the files relate to each other in relation to the filesystem path structure. Since PHP is not limited by the webserver's document root, you can access files and directories outside of it. This is a common method of making a private directory to store things like uploaded documents or include files.
  18. If you have a CLI build of php (not cgi) then there are three constants defined: STDIN - Input stream STDOUT - Output stream STDERR - Error stream You can use the normal file functions with these constants, such as fgets/fread. while($quit != true) { echo "Please enter the number to find "; $input = fgets(STDIN); //Do something with $input }
  19. As mentioned, the reason your example works without get and set is because PHP allows you to make new properties on the fly. One way to override this, and something I do on most of my objects, is to define __get and __set: abstract class LockedObject { public function __get($nm){ throw new Exception('Property '.$nm.' not defined'); } public function __set($nm, $v){ throw new Exception('Property '.$nm.' not defined'); } } You can use __get and __set to enforce certain rules or perform certain operations on your protected/private members. For example, say your data member needed to be of a certain object, say DateTime. You could do: class Posting extends LockedObject { protected $mPostedDate; public function __set($nm, $v){ switch ($nm){ case 'PostedDate': if (!is_object($v)){ //Assume string, try and make a new date time $v = new DateTime($v); } if (!(i$v instanceof DateTime)){ throw new Exception('PostedDate must be a DateTime object or date string.'); } break; default: parent::__set($nm, $v); } } public function __get($nm){ switch ($nm){ case 'PostedDate': return $this->mPostedDate; default: return parent::__get($nm); } } } With that, you can access the posted date property as if it were a public property ($obj->PostedDate). When you assign something to the property it will automatically validate it by attempting to convert any non-object value (for example a string such as '2011-11-25') into a DateTime. It will then only allow a valid DateTime object to be finally assigned to the property, anything else will cause an exception.
  20. It just removes what is an unnecessary check. fetch_object would return false in the case of no rows, there's no need to have a separate check for num rows=0 No, you want =, not ==. The way the condition would read is: if $result is false (ie, query failed ) or the return value of fetch_object (which gets stored in $row) is false (ie, no rows) then return false. The !$result is evaluated first, if it is true then the if is run and the function returns false. If $result is ok, then PHP will execute the ($row=$result->fetch_row()) part, which will get the first row and assign it to $row Then PHP tests if the result of that operation is false (the preceding !) and run the if causing the function to return false.
  21. The num rows check is not really necessary. Also, do not use the @ operator. It hides any errors which 99% of the time you do not want to do. Configure your script to log errors to a file rather than display them to the screen if necessary, do not just hide them all together. $result = $conn->query($query); if (!$result || !($row = $result->fetch_object())) { return false; } return $row->admin; Do as suggested, dump the value of $admin and exit the script so you can see it. Just because your script is supposed to redirect doesn't mean it has to while you debug. Just have it print debug info and die. var_dump() can be better than a direct echo as it will also show things like null/false more clearly
  22. The complex notation can be used around any variable in about any situation. It's generally not required however, except in instances where your variable name may be ambiguous or contains characters not normally allowed in a name. In your original case, without the braces: $controller->$url[1](); That code could mean two things: 1) Find the variable $url and get it's value. Use that value as the name on $controller, get it's [1]'th element then call it as a function. -or- 2) Find the variable $url and get it's [1]th element, use that as the name of a method on $controller and call it The author wants the latter, and the braces tell PHP to do exactly that. Other cases my be something like if your putting a variable in a string next to some text, eg maybe: $hiliFront = '<strong style="background: yellow;"><em>'; $hiliBack='</em></strong>'; echo "Welcome {$hiliFront}Kicken{$hiliBack}, how are you today"; //Without the braces, PHP would see the first var as $hiliFrontKicken instead of $hiliFront Or if a property on an object contains invalid characters, such as say: $sql = 'SELECT COUNT(*) FROM bleh'; $obj = mysql_fetch_object(mysql_query($sql)); echo $obj->{'COUNT(*)'}; //Though a better solution is to alias the column name
  23. No, you can't just add arguments. You can inline the array's if you want, but if your number of replacements gets large it's more readable to have them separate. str_replace(array(' ', '-'), array('cars', 'bikes'), $row['model']);
  24. No. -- and ++ work on the variable directly. Using the variable that you apply the ++/-- to elsewhere in the same statement generally falls under the category 'Undefined Behavior'. This means the computer can do whatever it wants and it'll be considered a valid response to the operation. I'm not sure if php defines this behavior or not. I know for instance C does not. The statement $a = $a--; could be interpreted as multiple different actions sequences, which each would have separate outputs. Assume $a=10 to start with, it could be: $a=10; $tmp = $a; //save the value of $a for use later. $a = $a - 1; // the -- operation $a = $tmp; //use the previous value of $a //final result is $a = 10. The -- is essentially nullified. or it could be run as: $a = 10; $a = $a; //Use the current $a; $a = $a - 1; //run the -- operation //final result is $a=9.
×
×
  • 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.