-
Posts
60 -
Joined
-
Last visited
Everything posted by marcbraulio
-
Hello everyone, I am trying to select and return any rows that have a specific string in it. Let's say I have a column name "item_id" and in every row of that column I have values like so: row A: (11,22,44,34) row B: (12,56,78,98) If the user picks the number '22', I use the following SQL line to get the row in which the number '22' is contained. SELECT * FROM Persons WHERE City LIKE '%22%' and row A gets returned, just like it is suppose to. The problem is, if the user selects a single digit number like '2', both row A and row B gets returned because they both have values that contain the number '2' in it. Instance, row A has the number '22' and row B has the number '12' so both rows get returned when the number '2' is requested. I don't want that, I only want the row that has the exact number '2' or '34' or whatever number I request, to be returned. So to get around this problem, I am wrapping my numbers in letters like so: row A: (a11a,a22a,a44a,a34a) row B: (a12a,a56a,a78a,a98a,a2a) and requesting it like so: SELECT * FROM Persons WHERE City LIKE '%a22a%' My question is: Is there a better way to work out this logic?
-
Assigning variables to themselves using curly braces
marcbraulio replied to marcbraulio's topic in PHP Coding Help
Thank you for the insight, I'll be definitely following the advice. Will using array assignments like so: foreach($items as $item) { $my_array[$item] = $item; } also affect performance? I am just trying to avoid using things like "my_array[0]" for readability purposes. -
No problem, best of luck!
-
I didn't know how to describe this, so please excuse my title. As most of you know, this is possible: foreach($items as $key => $item) { ${$key} = $item; } // I would then be able to access it like so: $key // or whatever the value of "$key" is. However, how could I append something extra to the ${$key} variable? Instance: foreach($items as $key => $item) { $my_{$key} = $item; // or ${$key}_arr = $item; } // So that the end result of my variable becomes like so: $my_key // or $key_arr // Instead of just $key // or whatever the value of "$key" is. Is there a known method for this? Any input is appreciated, thank you.
-
Well object oriented programming is where the future is headed, so mines well start now =]. But yeah there are some upsides to it, besides it being easier to type, you can do some cool things with it like load data directly into a class for direct manipulation. If you ever get curious, take a look here: http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
-
for ($i=2012; $i>=1900; $i=$i--) { echo "<option value='$i'>$i</option>"; $bday[] = $i; } if (!in_array($_POST['BirthDay'], $bday)) { message("Please select a Correct Birthday, or u trying to hack"); } Couple of things... 1. Why not use "$i=$i--" instead of "$i=$i-1"? 2. Why would you want to dynamically make an array of something that you know for a fact is not going to change? Save yourself some resources and do a static array of the years instead of running it every single time. 3. I really hope that is not the kind of error messages you give your users.
-
Or try the object oriented approach: try { $stmt = $conn->prepare($sql); $stmt->execute(); $results = $stmt->fetch(PDO::FETCH_OBJ); $stmt->closeCursor(); } catch(Exception $e) { die($e->getMessage()); } You can easily access it like so: echo $results->field_name;
-
Why are you not allowed to use "Default" as a function name?
marcbraulio replied to marcbraulio's topic in PHP Coding Help
Understood, thank you! -
Sessions are unique to each user and because they are unrelated to any other user there is no chance of a mix up, regardless if multiple users submit the information at the same time. If I was you, I would call a session destroy after the information is displayed back to the user, just in case. If you have no need to store their information than just use sessions instead of using the database. In that case, you can't rely on prepared statements to do the sanitizing for you.
-
Does this have something to do with you trying to connect with facebook?
-
Inserting multidimensional array value into mysql
marcbraulio replied to Drongo_III's topic in PHP Coding Help
Lol, I guess some one will just have to call up Rasmus Lerdorf for a full explanation. But also be aware of the use of curly braces in variables in order to separate it for from the rest of the text. For instance: $dona = "connec"; $dona = "educa"; $dona = "presenta"; $dona = "evolu" // normally you would do: $myVar = 'The word' . $dona . 'tion could have a different beginning but always ends the same way.'; // but alternatively you can do (greedy token parsing): $myVar = "The word {dona}tion could have a different beginning but always ends the same way."; So in order to separate $dona from "tion" you use the curly braces, thus avoiding php from reading the string as $donation. Figured I would point this out for informational purposes considering it is probably closely related to the use of concatenation and curly braces in the array. -
Inserting multidimensional array value into mysql
marcbraulio replied to Drongo_III's topic in PHP Coding Help
That is actually a very good question, I am tempted to say it has something to do with greedy token, but that wouldn't make any sense. If anyone knows please share! But alternatively you can use braces like so (it's more readable in my opinion): "INSERT INTO teams (company) VALUES ('{$myArray[0][3]}')" Or you can just asign the array value before hand and insert a regular string like so: $myValue = $myArray[0][2]; "INSERT INTO teams (company) VALUES ('$myValue')" -
Here is an inefficient solution, but it is still a solution: <?php $conn = new PDO('mysql:host=localhost;dbname=test', 'root', 'password'); $sql = "SELECT hr FROM 2012hitting UNION SELECT hr FROM 2013hitting"; $results = $conn->query($sql); while ($row = $results->fetchObject()){ $hr_array[] = $row->hr; } $total_hr = $hr_array[0] + $hr_array[1]; echo $total_hr; Note: this is a PDO connection as oppose to the the regular mysql connection you are using. The concept is still the same. If you decide not to change your table structure like "Psycho" suggested, which I highly recommend you do as it will make things much easier, look into the UNION and JOIN methods for SQL to make things work with the structure you currently have.
-
lol, haven't heard that one before... Nways, I understand. Look into the PHP Session function, you can most likely accomplish what you want by using sessions and good php logic. If you are not familiar with sessions, here's a good intro: http://www.youtube.com/watch?v=hPX_g_hcuH8&feature=relmfu
-
I am no expert but I don't think there is simple way to accomplish something like that with php alone. I would say that the easiest method would be to use AJAX to send the request, compare the results to the current information in the cart, then update it. Something along those lines. If you post an example code with some kind of pre-defined logic, I can probably help you further.
-
Getting the base url of the installation
marcbraulio replied to marcbraulio's topic in PHP Coding Help
Worked perfectly, thank you! Now that I think of it, another alternative would be to just get the path of the main index.php file, which will always reside in the main directory of the installation. Out of curiosity what is the best method of getting the path of a particular file? Also, if it is not too much of a hassle, could you explain what exatly is going on in this line: $basedir = substr($_SERVER['REQUEST_URI'], -1)=='/'?$_SERVER['REQUEST_URI']:dirname($_SERVER['REQUEST_URI']); -
If you are learning from scratch, I would say look into nettuts for lessons, specifically look for an author named "Jeffrey Way". He has made some of the most comprehensible and user friendly tutorials I have ever seen, on a broad range of subjects. My claim is debatable, of course, but look into it, you have nothing to loose. As far as PHP assistance goes, through out my search, I found phpfreaks.com to be the most active and responsive forum out there. Not to mention the exceptional community and quality of the responses I have received so far. If you are looking to specifically learn PHP and MySQL, look into a book called "PHP Solutions Dynamic Web Design Made Easy", it is an amazing introduction for beginners, unlike many books out there, it doesn't make the assumption that you know what you are doing, thus it explains each step in depth. Last but not least, Google is your friend.
-
Getting the base url of the installation
marcbraulio replied to marcbraulio's topic in PHP Coding Help
The code for each directory is located in its main directory, instance: /home/test is a complete CMS by it self. /home/test/test2 is another complete CMS by it self and /home/test/test3 is another complete CMS by it self. The problem I am having is that the CMS installed in "/home/test/test2" and "/home/test/test3" will output the base url of "/home/test/" instead of their real base directory which are "/home/test/test2" and "/home/test/test3". Same for the CMS installed in "/home/test" which will output "/home/" for its base url. When you tested the code, did you get /home/test -> /home/test /home/test/test2 -> /home/test/test2 /home/test/test3 -> /home/test/test3 for each one? -
Understood, thank you very much for the in-depth explanation. On a side note, this may seem like a bit of an unorthodox request, but seeing that you have an immense amount of posts/rep, do you think you could take a look at my other post regarding "base url" in php? I have been going at it for a few hours and I am breaking my head over this. I am very sorry if this causes any offence, this will be the only time.
-
Getting the base url of the installation
marcbraulio replied to marcbraulio's topic in PHP Coding Help
Thank you for the quick reply. It is very close, but not quite there yet. Here are the results: http://www.example.com/CMS/ will output: http://www.example.com http://www.example.com/CMS/CMS-2 will output: http://www.example.com/CMS http://www.example.com/CMS/CMS-2/CMS-3 will output: http://www.example.com/CMS-2 Seems like it is a directory behind. -
Hello, I am attempting to get the base URL of the directory that the CMS was installed, not the main directory. Here's what I mean: For example, let's say my domain is http://www.example.com but I install my CMS under http://www.example.com/test/cms and on another occasion I install it under http://www.example.com/cms, and on another occasion http://www.example.com/my/new/cms/ I would like my base url for the http://www.example.com/cms installation, to be "http://www.example.com/cms" and my base url for the http://www.example.com/test/cms installation to be "http://www.example.com/test/cms" and so on, without me having to manually change anything. How do I go about obtaining the directory that corresponds to that particular installation? I know it is possible because Joomla does it automatically. I have already tried numerous combinations using $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'], and so on, no luck.
-
Aside from being easier, this is more secure as well right?
-
Hello everyone, What is the best method of blocking direct access to certain files like functions, modules, and etc? I was trying the if ( ! defined('BASEPATH')) exit('No direct script access allowed'); method but I feel like there must be a more convenient/better way. Any suggestions are appreciated, thank you.
-
Foreach vs While loop in PDO Database retrieval
marcbraulio replied to marcbraulio's topic in PHP Coding Help
For the most part I am sticking with the object form, just for consistency and convenience. If I can access it directly ($var->title), why bother using arrays. Anyways, here's some really good insight for those who are interested: http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html Just do a quick "CONTROL + F" and type in "Fetch Modes".