Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

String replace

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

String replace

Postby okoa » Sun May 12, 2013 9:06 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?
okoa
New php-forum User
New php-forum User
 
Posts: 8
Joined: Wed May 08, 2013 9:18 am

Re: String replace

Postby swarups » Sun May 12, 2013 10:15 pm

Hi,

I guess, a student will have more than one subject and grade as well. But here in your code while retrieving the data from the database, you are storing the data to a variable. So in each loop the variables gets updated. In this way it will have the value of last subject and grade.

So here you need to store the data(subjects and grades) in an array. Then in cert file just loop thru the array and write the subject and respective grade.

I guess this will help you.

Thanks
Swarup Sahu
http://www.mindfiresolutions.com/
swarups
New php-forum User
New php-forum User
 
Posts: 20
Joined: Tue Mar 06, 2012 11:05 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron