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


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

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 == !!!

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

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.


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