-
Posts
579 -
Joined
-
Last visited
Everything posted by Drongo_III
-
PHP to MYSQL - Putting a PHP Array into a MYSQL table??
Drongo_III replied to PHP Learner's topic in PHP Coding Help
Yeah agree with miko that what i've suggested is a dirty way of doing it and concentrating on proper structure in your tables is the preferable route to go down. It was just the simplest way of doing it imo if the task is a small one but if your database is likely to grow a lot then yes, contentrate on table structure and let the database do the processing. -
Hi Guys Another noob question. I have written a script to create a csv file from a mysql query. Bread and butter you might think... The script i've written (based on reading a few bits and a few bits there) creates the csv file in the same directory as the php script. I don't really want this to happen because the data it's pulling is a little sensitive. So my questions: [*]How can i stop the csv from storing itself locally? [*]How can i output the csv directly to the browser - i.e. to initiate a download automatically [*]You'll notice that in the code I have a while loop within which i run a function to trim the data results as they are pulled down before placing them in the csv. I suspect that recursively calling this function is probably not the most efficient way of doing it. Any suggestions on how this should be done? I don't expect you to code it (unless you really want to) just an explanation to point me in the right direction would be fab. Any help would be very appreciated. Drongo <?php require_once("config.php"); $select = "SELECT * FROM members"; $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) ); $fields = mysql_num_fields ( $export ); for ( $i = 0; $i < $fields; $i++ ) { $header .= mysql_field_name( $export , $i ) . ","; } echo $header; $headings = explode(",", $header); //Set headers in first line of csv. $fp = fopen('test.csv', 'w'); fputcsv($fp, $headings); fclose($fp); // Function to trim all values. Remember the pass by reference to change original value. function trim_all(&$value) { if (is_array($value)) { array_walk_recursive($value, 'trim_all'); } else { $value = trim(str_replace("\r\n", "\n", $value)); } } //open file for writing $dp = fopen('test.csv', 'a+'); // ignore this line mysql_data_seek($export, 0); // while loop runs trim on data and stores each array as csv row while($rows = mysql_fetch_row($export)) { array_walk_recursive($rows, 'trim_all'); print_r($rows); fputcsv($dp, $rows); } fclose($dp); ?>
-
Call php script when a link is clicked
Drongo_III replied to houssam_ballout's topic in PHP Coding Help
Then you can probably do this a simpler way if that's all you need to achieve. So you very simply change the link to be <a href="YouScript.php?inc=1" id=""> Your Link </a> All you're doing here is sending the user to your script page and we've tacked on a little query string on the end of the link to act as a flag to the script (query string is the ?inc=1 bit) so you know the person clicked this link and didn't just type in the uri. The scrip then runs your db query and send the user to the facebook link. Then on your script page you just do <?php if(isset ($_GET['inc'])){ //Check to see if the inc var is set to be sure someone has clicked the link // Run database query to increment your counter. Can you do this? //Once you have run all database stuff successfully then change // header location to redirect the user header('Location: http://www.facebook.com/ShareSomething'); } else { //If the inc variable isn't set then send them back to your site header('Location: http://www.BackToYourSite'); } I assume you can work out the database bit? Drongo -
Call php script when a link is clicked
Drongo_III replied to houssam_ballout's topic in PHP Coding Help
Hi Mate By the way - you can download the jquery library from jquery.com What exactly are you trying to achieve? If they click a link what is meant to be updated? -
Call php script when a link is clicked
Drongo_III replied to houssam_ballout's topic in PHP Coding Help
You need to download jquery and save it in a file with .js extension. You can get it from the jquery website. The ajax obviously won't work without the jquery library. Sorry probably should have made that clearer. Drongo -
PHP to MYSQL - Putting a PHP Array into a MYSQL table??
Drongo_III replied to PHP Learner's topic in PHP Coding Help
Hi I'm not 100% following what you're trying to acheive but... If you want to store multiple values in the same database record from an array, and you want it to be easily readable, you could implode your array before storing it. $comma_separated = implode(",", $your_array_name_here); The delimter in this case is the comma. So you could add additional records stright to the database by just adding a comma. Then when you pull the record from the database again you can just use the explode function to separate it backout into array values. Probably not the most sophisticated way of doing it but a possible solution Drongo -
That would likely be best achieved using jquery mate because it's not really dynamic - you're just shuffling data that's already been generated and not actually pulling new data. At any rate checkout jquery http://api.jquery.com. I think there are probably a few ways to acheive what your example does. One would be an ajax call to a script that reproduces the table based on what has been clicked. Or a less resource intensive method would be to create a jquery script to pull out the table values into an array and remake the table based on the desired sort option - though that would need some thought to pull off correctly. Hope that helps a little... Drongo
-
Pull amount of rows in MYSQL table and display?
Drongo_III replied to jackmcnally's topic in PHP Coding Help
Sounds like you want: $result = mysql_query("SELECT * FROM tablename", $con); $num_rows = mysql_num_rows($result); echo $num_rows . " " . "have signed up. Will you? "; Num rows counts how many rows of data you have in your specified table. http://uk.php.net/manual/en/function.mysql-num-rows.php Drongo -
Call php script when a link is clicked
Drongo_III replied to houssam_ballout's topic in PHP Coding Help
Hi mate To be more helpful This represents your page with the link: <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="jquery.js" ></script> </head> <body> <script type="text/javascript"> $(document).ready(function() { $("#a").click(function() { $.ajax({ type: "POST", url: "ajax.php", data: "name=John&location=Boston" }).done(function( msg ) { alert( "Data Saved: " + msg ); }); }); }); </script> <!-- YOUR TEST LINK --> <a href="#" id="a"> TEST LINK </a> </body> </html> Ajax bit explained The type: POST - simulates posting the data - just like posting from a html form. URL: This is the php script you want to send the data to. Data: This is the data you want to send to the script. This is hardcoded below but could be represented by variables. Done function : This is the function that will be executed once your ajax call has been successful and it will return whatever you send from the php script. So you can do anything to your page here. PHP Script - ajax.php <?php $name = $_POST['name']; //access the post data you send via the ajax script echo "$name"; // whatver you echo or return will be sent back to the callback (Done function). ?> That php script obviously just illustrates simply how it works. But you could do a switch statement to see what variable is being sent in by the ajax and that can then make a call to a specific function to perform an action relative to the link clicked. Hope that helps. If you need any more clarification just shout Drongo [/code] -
Call php script when a link is clicked
Drongo_III replied to houssam_ballout's topic in PHP Coding Help
Hi mate This would need to be an ajax call. Easiest way to do it is with jquery. Check out this link for info http://api.jquery.com/jQuery.ajax/ Drongo -
Understanding get, set and property overloading
Drongo_III replied to Drongo_III's topic in PHP Coding Help
Thanks for your replies guys. I think it's a hard thing to wrap your head around at first but your examples have helped me understand it a bit better - especially where you've mentioned setting private variables. What i wasn't taking into account is the logic you put into the methods which makes them infinitely more useful. I guess this can help make your code a lot more secure if you write your __get/__set correctly. Thanks a lot for taking the time to respond chaps. Drongo -
Does that happen on the flat html file or just when you use an include? To be honest, and some may disagree here, but if you're using a strict Doc Type you're going to have to be a lot more sharp in your coding. I generally always use a transitional doc type - i.e.: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> You may find it more forgiving. Let me know if that works Drongo
-
I did I was just trying to explain the process of how i came the realisation which for me was quite gratifying hehe!
-
Hi PFM Thanks for the spots there. I will sort the issues with the loop now and starting getting into the habit of turning all errors on too. I found an unnecessary fix for the problem in using: mysql_data_seek($export, 0) ; And after you mentioned the code before is likely causing the issue i had a realisation! I had already called mysql_fetch_row further up in my code. So i am guessing that was pushing the database pointer on one record which is why the first record was always getting missed by the loop. For some reason i assumed that a new call to the fetch the row would reset the pointer but thinking about it that would probably be very silly indeed! Thanks for your advice. Very grateful. Drongo
-
I am trying to recursively pull records from a database to write to a csv. There are three test rows of data in my table (and no not including the table headers). The only issue is i can’t seem to get my for loop to display all the records – it only displays the last 2 rows so i’m very confused. Anyone suggest why this isn’t working? $num_rows = mysql_num_rows($export); for ($i=0; $i <= $num_rows; $i++ ) { $row = mysql_fetch_row($export); echo $row[1] . $row[2] . $row[3] . $row[4] . $row[5] . $row[6] ."<br/>"; echo $i; } Incidentally the query is just a fetch all from datasbase. Anyone see where i'm going wrong? Thanks, drongo
-
Understanding get, set and property overloading
Drongo_III replied to Drongo_III's topic in PHP Coding Help
Anyone able to help me on this one? -
Thanks Kicken That really helps a lot to understand it's use. I suppose part of it is understanding how php will interpret the syntax and what it interprets first - and thinking in that way. I can see much more clearly that the curly braces allow for some very handy flexibility. I was trying all sorts to make that function call work without using curly braces and nothing did the trick. Think this is one to etch on the noggin! - or in english, 'remember'. Thank you so much for your help. This forum has helped me learn so much it's amazing
-
Thanks guys That really helps a lot. I had not come across this way of calling a function in a class before and i can't bring myself to just accept that it works without understanding it as its obviously not the best way to learn. So thank you very much for setting me straight. Drongo
-
Can't you just use class second extends master Then you should be able to access everything from the master class in the second class. Or maybe i've misunderstood you :/
-
Hi Guys Quick noob question. I am still fairly new to oop and i'm trying to also build my first simple mvc to learn about that too. I came across some syntax use in a tutorial that works but i don't really understand why and don't know what this is called to search for it on google. I've instantiated the class and then i pull out and explode the url to make controller or function calls in the mvc. The noob bit i don't understand is this $controller->{$url[1]}(); Why do the curly brackets allow you to place a variable name? And what is this type of syntax called so i can read about it. Any help would be massively appreciated! Drongo
-
Hi mate The main two things you need to do are to unset the session variables and destroy the session. These two functions are the key: //remove all the variables in the session session_unset(); // destroy the session session_destroy();
-
I think you're good to just place it directly below your first php closing tag mate
-
Hi Guys I’m trying to get my head around the magic functions __get and __set and property overloading. From what I’ve read this sounds like a really useful thing to learn/use in the right scenario but the examples I have seen have all confused me greatly because I can’t see that the code is actually doing anything. Take the snippet below (an example of Devshed). I can follow the logic of the get set bit. class User { // constructor (not implemented) public function _construct(){} // set undeclared property function __set($property, $value) { $this->$property = $value; } // get defined property function __get($property) { if (isset($this->$property)) { return $this->$property; } } // example of usage of 'User' class with property overloading $user = new User(); $user->fname = 'Alejandro'; $user->lname = 'Gervasio'; $user->email = 'alejandro@mydomain.com'; $user->address = 'My address 1234'; // display user data echo 'First Name: ' . $user->fname . ' Last Name: ' . $user->lname . ' Email: ' . $user->email . ' Address: ' . $user->address; /* displays the following First Name: Alejandro Last Name: Gervasio Email: alejandro@mydomain.com Address: My address 1234 */ } The part I don’t understand is where you assign something to a variable i.e. $user->fname = 'Alejandro'; Because even if I wiped out everything in the class (so there is no __get __set functions) and then just did $user->fname = 'Alejandro'; echo $user->fname; It still shows "Alejandro" in the browser. So I can’t see that the __get __set is actually doing anything at all. So am I missing the point and doing something wrong (likely)? Or is this just a bad example? Can someone help me to understand please Drongo
-
So changing this: #navlist a:link#current, #navlist a:visited#current, #navlist a:hover { border-bottom: 4px solid #66733f; padding-bottom: 2px; background: transparent; color: #4F4F4F; } /* this line's the style for the current page link */ doesn't change anything? Have you tried wiping out the css on that line just to see if it has any effect and to ensure it's not just a typoed syntax issue? If you do delete that line and it's still displaying that style then I would guess the style is being applied elsewhere - perhaps inline via the included menu.php?? Got a link to the website? Drongo
-
Hi Mate I'm no php superstar liek some of the guys here but you could try somethnig like this: //this assumes you've already connected to your DB $sql = "SELECT promo FROM TABLENAME WHERE id = USERID"; $result = mysql_query($sql); $promo = mysql_fetch_assoc($result); echo $promo[promo]; //Or if you wanted to assign it to the sessino then: $_SESSION['promo']= $promo[promo]; //then you could do echo echo $_SESSION['promo']; Incidentally just writing this off the top of my head so i can't be 100% it works without testing it but something along those lines.