SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: PHP Script file

  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Script file

    Hi there

    I am trying to write a function that allows me to pass 4 variables table name and WHERE and preheading for any html code i want to pass and the html code for inside my while loop

    PHP Code:
    function Display($table,$WHERE,$prehead,$return)
    {
        
    //
        
    global $con;
        global 
    $data;
        
    $Query="SELECT * FROM $table WHERE $WHERE";
        
    $rs=mysqli_query($con,$Query);
        if(!
    $rs)
        {
            echo 
    "Error".mysqli_error($con);
        }
        else
        {
            
    //
            
    $count=$rs->num_rows;
            if(
    $count=1)
            {
                if(!
    $prehead == "")
                {
                    echo 
    $prehead;    
                }
                while(
    $data=$rs->fetch_assoc())
                {
                    
    print_r($data);
                    
    //3aecho 
                    
    echo "Pre".$return."";
                }
            }
            else
            {
                echo 
    "No Such Records";    
            }
        }
        
    //    
    }
    Display("members","username='demo'","<h1>Showing Member information of</h1>","<table>
        <tr>
            <th>Member</th>
            <th>"
    .$data['username']."</th>
        </tr>
        <tr>
            <th>Rank</th>
            <th>"
    .$data['rank']."</th>
        </tr>
        <tr>
            <th>email</th>
            <th>"
    .$data['email']."</th>
        </tr>
    </table>"
    ); 
    When i pass $data['username'] etcc through it

    How come its returning back as $data not found or null?

    Thanks,William

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Rule of thumb is not to have functions echo. But, lets ignore that for the moment.

    #1: Pick a style, procedural or object. You've got mysqli_query (procedural), and then use the result as an object (... object.).

    #2: if($count=1)
    Danger Will Robinson. = is not the same as == !!!!!

  3. #3
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay thanks

    but why isnt $data['username']; and rank and email not being displayed their values upon output when i go echo $return?

    Thanks,William

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Because you're passing a string - it's already parsed.

    when it enters the function, the variables will already have been replaced by the values of the variables before execution.

    What you're trying to do is templating - try this.

    PHP Code:
    function Display($table,$WHERE,$prehead,$return)
    {
        
    //
        
    global $con;
        global 
    $data;
        
    $Query="SELECT * FROM $table WHERE $WHERE";
        
    $rs=mysqli_query($con,$Query);
        if(!
    $rs)
        {
            echo 
    "Error".mysqli_error($con);
        }
        else
        {
            
    //
            
    $count=$rs->num_rows;
            if(
    $count=1)
            {
                if(!
    $prehead == "")
                {
                    echo 
    $prehead;    
                }
                while(
    $data=$rs->fetch_assoc())
                {
                    
    print_r($data);
                    
    //3aecho 
                    
    foreach($data AS $key => $value) { 
                        
    str_replace("#".$key."#",$value,$return);
                    }
                    echo 
    "Pre".$return."";
                }
            }
            else
            {
                echo 
    "No Such Records";    
            }
        }
        
    //    
    }
    Display("members","username='demo'","<h1>Showing Member information of</h1>","<table>
        <tr>
            <th>Member</th>
            <th>#username#</th>
        </tr>
        <tr>
            <th>Rank</th>
            <th>#rank#</th>
        </tr>
        <tr>
            <th>email</th>
            <th>#email#</th>
        </tr>
    </table>"
    ); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •