Jump to content

Drongo_III

Members
  • Posts

    579
  • Joined

  • Last visited

Posts posted by Drongo_III

  1. 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.

     

    :)

     

     

     

     

  2. 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);



    ?>

  3. 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

     

     

     

     

    for example look:

     

    I had this link: <a href="http://www.facebook.com/ShareSomething">Click here to Share</a>

    So when the user clicks on that to share an item, then I need to increments the shares by 1 ,& direct him to the page.

  4. 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?

     

     

    Also, when the user click on the link the php script will be called which will update the database & the user will be directed to the requested URL

    thanks for your support :)

  5. 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

     

     

    I had the html in a file and ajax.php now when I click on the link, I can't see it working.

    where the javascript file that is included in the html file?

    Thanks

  6. 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

     

  7. 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

     

    Sorry...this is want I want to do

    http://www.dynamictable.com/

  8. 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]

     

    is there any example related to my requirements.

    thanks

  9.  

    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

     

     

     

     

     

    Hello,

    I had a web page which had sharing (share issue on facebook, twitter....) and I want to record what each user had shared. So, they will click on alink to share it. I want to call a php function when the user click on a link, whats the best method to do this?

    Thanks in advance

  10. 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

  11. 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

     

    For some reason doctype removes my css effects (only on IE)

  12. 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

     

     

     

    Best guess is that in the code that is before the code you did post, you are fetching and discarding the first row from the result set. Posting all the code from the query through to the end of your loop is the best way of getting the quickest solution.

     

    The code you did post is also producing errors on the last pass through the loop because of the equal comparison in the loop condition (and that you are starting the loop at zero.) You should only be using less-than <

     

    You should also have php's error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects.

  13. 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

  14. Anyone able to help me on this one?  :confused:

     

    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

  15. 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 :)

     

     

     

     

    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
    

  16. 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

     

     

     

     

    To be more verbose...

     

    You can call a function in two ways:

    1) 

    $someClass->myFunction();

     

    2) 

    $functionName = 'myFunction';

    $someClass->{$functionName}();

     

    What you are seeing in this code is an array of strings, one of which is being used as a function name.

     

    (There are actually many more ways to call functions, I'm just trying not to break your brain).

  17. I have a odd one here, two classes and one which calls another class.

    I want to pass all of the parameters from the array as seperate variables into the next function. Hope my example explains a little better

     

    class master

    {

      public function __call($name, $args)

      {

        second->$name($args);

      }

    }

     

    class second

    {

      public function example($param1, $param2)

      {

     

      }

    }

     

    master->example($param1, $param2);

     

    ( i know its not accurate php, but should get the idea of what im trying to do)

    I need to do something with the following line

    second->$name($args);

    Where param1 and param2 will be seperated out

    Is there a php function which you can tell it the class, method name and pass it an array of parameters?

     

    Thanks

     

     

    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 :/

  18. 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

     

     

  19. how to logout using session in proper way ? can you give me the code

     

    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();  
    
    

  20. I think you're good to just place it directly below your first php closing tag mate :)

     

     

    Hello again:]

     

    I am wondering where should i put valid doctype

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    and meta tags

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

     

    in a php file like this.

     

    <?php
    
    session_start(); // Session starts here
    if(!isset($_SESSION['username'])) // If there is no session then...
    {
        die ("<div align='center'><img src='images/LOGINKITTEN.jpg'/><br /><a href='index.php'><img src='images/Login.jpg'/></a></div>"); // ...Die!
    } 
    
    $id = $_GET['id'];
    
    ?>
    <html>
    <head>
    
    <link rel="stylesheet" type="text/css" href="jamcss.css" />
    
    </head>
    <body>
    
    <img src="images/jamspace.jpg" />
    <div id="jamcontainer">
    <b><a href="main.php">Home</a>  ||  <a href="members.php">Members</a>  ||  <a href="jams.php">Jams</a>  ||  <a href="uploadjamform.php">Upload Jam</a>  ||  <a href="myprofile.php">My profile</a>  ||  <a href="about.php">About</a>  ||  <a href="adminindex.php">Moderators</a> ||  <a href="logout.php">Logout</a></b>             You are logged in as <?php echo $_SESSION['username']; ?>
    <br /><br />
    
    <?php
    
    $connection = mysql_connect("lol.com", "loldb", "lol00") or die ("Could not connect");
    mysql_select_db("loldb", $connection) or die("Could not connect");
    
    // mysql query to get the username
    $query = mysql_query("SELECT * FROM news ORDER BY id DESC");
    $numrows = mysql_num_rows($query);
    
    
    
    if ($numrows!=0)
    {
    // fetching data from the database to variable
    while ($row = mysql_fetch_assoc($query))
    {
    	$title = $row['title'];
    	$post_owner = $row['post_owner'];
    	$post = $row['post'];
    	echo "<b>" . $title . "</b><br />";
    	echo "Posted by <b>" . $post_owner . "</b><br /><br />";
    	echo $post . "<hr></hr><br /><br />";
    }
    }
    
    ?>
    </div>
    </body>
    </html>
    

  21. 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

     

  22. 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

     

     

  23. 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.

     

    Hello all,

     

    I have a membership website which is using sessions... and ive been asked to add some promotion points system. So that each user is able to see how many promotion points they have...

     

    Now, I'm a beginner in mysql and php, but feel I'm learning fairly quickly. What I need help with, is to be able to display the amount of promotion points for the logged in user.

     

    I created a new field in my "essenti1_Users" table for the promotion code.

     

    database is called "essenti1_membership"

    table is "essenti1_Users"

    feild is "promo"

     

     

    I think im going to have to manually add the points to each user manually through phpMyAdmin Navicat unfortunatly. Unless anyone has any other ideas just for adding the points to each user account?

    ziggynerja is online now Add to ziggynerja's Reputation Report Post  Edit/Delete Message

×
×
  • 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.