Replace list of strings with database table values

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

okoa
New php-forum User
New php-forum User
Posts: 8
Joined: Wed May 08, 2013 9:18 am

Replace list of strings with database table values

Postby okoa » Wed May 08, 2013 9:46 am

Hi everyone!
I am stuck and I need some fixing. I am creating a certificate generating system. the certicate contains subjects which should be replaced with grades per each student stored in mysql table. The student details, subject and the grade per each subject is stored in table results. I have writen the following code:
<?php
$query_getResults="select grade from results where studentnumber='$studentnumber'";
$result=mysql_query($query_getResults);
$rows=mysql_num_rows_($result);
$i=0;
while($i<$rows)
{
$subject=mysql_result($returnresults,$i,Subject);
$grade=mysql_result($returnresults,$i,Grade);
I want to replace subject1 with corresponding subject name from th database , then subject2 and so forth. I also want once the <<subject>> place holder is replaced, should be assigned the relevant grade from the database.
<<subject1>><<grade1>>
<<subject2>><<grade2>>
<<subject3>><<grade3>>

I want the string above to look like the one below after replacement with database values:
Maths A
English B
Computer studies B+
I wrote the code below but cannot not work, can only replace with a single value. The array seem not to work for me.
@header("content-type:application/msword");
@header("content-Disposition:inline, filename=cert.rtf");


//open template file
$filename="certs.rtf";
$fp=fopen($filename,"r");
//read the template into a varriable
$output=fread($fp,filesize($filename));
fclose($fp);
//replace the place holders in the template with new data

$output=str_replace("<<subject1>>",$subject,$output);
$output=str_replace("<<grade1>>",strtoupper($grade),$output);
$output=str_replace("<<subject2>>",$subject,$output);
$output=str_replace("<<grade2>>",$grade,$output);

//generate output
echo $output;
?>
Any assistance please?

Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron