Jump to content

Recommended Posts

I have a problem in writing PHP, I hope you can help me.

 

my file:

---------------------------------------------------------------------

<?{

if($bbcode==7010001) $bbname="Siu Ho Kiu";

elseif($bbcode==7010002) $bbname="Jim Ho Chun";

elseif($bbcode==7010003)$bbname="Poon Cheuk Yin";

else{$bbname="Please try again!";}

}?>

 

<table align=center cellpadding=0 cellspacing=0><tr><td>

<? {($bbcode=7010001);?>

7010001<br>BB: <?=$bbname?>

<?}?>

 

<? {($bbcode=7010002);?>

7010002<br>BB: <?=$bbname?>

<?}?>

 

<? {($bbcode=7010003);?>

7010003<br>BB: <?=$bbname?>

<?}?>

</td></tr></table>

---------------------------------------------------------------------

 

Kindly please help me to make the result as below:

 

---------------------------------------------------------------------

7010001

BB: Siu Ho Kiu

7010002

BB: Jim Ho Chun

7010003

BB: Poon Cheuk Yin

---------------------------------------------------------------------

 

Thanks with Best Regards,

QEgg

Not too understand about your question. Are you pulling those data (bbcode and bbname) from the database? Or issit prefixed? which mean only 3 data?

 

Try use loop:

for($i=7010001; $i<=7010003; $i++){
    echo "<tr><td>";
    echo $i . "<br />";
    //here you may include your if-else statement to check which code indicate which name and print it out.

    echo "</td></tr>";
}

 

Hope this help you.

If you just want to show code and name for a single code input

 

<?php
$bb = array (
          '7010001' => 'Siu Ho Kiu',
          '7010002' => 'Jim Ho Chun',
          '7010003' => 'Poon Cheuk Yin'
    );

$bbcode = '7010003';        // sample input value

$bbname = isset($bb[$bbcode]) ? $bb[$bbcode] : 'Please try again';

echo "<table align='center' cellpadding='0' cellspacing='0'>
        <tr><td>$bbcode</td></tr>
        <tr><td>BB: $bbname</td></tr>
      </table>";
?>

 

if you want to list all 3

 

<?php
echo  "<table align='center' cellpadding='0' cellspacing='0'> ";
foreach ($bb as $bbcode => $bbname) {
    echo "<tr><td>$bbcode</td></tr>
        <tr><td>BB: $bbname</td></tr>";
}
echo "</table>" ;
?>

Thanks for your reply.

 

However, what I want is... the scripts I wrote were failed as below:

7010001

BB: Please try again!

 

7010002

BB: Please try again!

 

7010003

BB: Please try again!

 

Instead of this:

 

7010001

BB: Siu Ho Kiu

 

7010002

BB: Jim Ho Chun

 

7010003

BB: Poon Cheuk Yin

 

For the whole picture, I would like to show like this:

 

7010001

BB: Siu Ho Kiu

Date of birth: 2007/1/15

Name of mother: Mrs Siu

Name of father: Mr Siu

Place: Kowloon

 

7010002

BB: Jim Ho Chun

Date of birth: 2007/2/2

Name of mother: Mrs Jim

Name of father: Mr Jim

Place: Hong Kong

 

7010003

BB: Poon Cheuk Yin

Date of birth: 2007/1/28

Name of mother: Mrs Poon

Name of father: Mr Poon

Place: TST

 

7010004...............7019999, etc

 

 

The head may be:

<?{

if($bbcode==7010001);

$bbname="Siu Ho Kiu";

$dob="2007/1/15";

$nameofmother="Mrs Jim";

.... etc

 

would you like to guide me how to make this fortmat? Thanks

Firstly, I recommend you store the data in a file rather than in the program code. That way you can add to the data without having to change the code. One way is csv file format.

 

Save this in names.txt, Each line contains "bbcode,familyname,givenname,dob,place" .

 
7010001,Siu,Ho Kiu,2007-01-15,Kowloon
7010002,Jim,Ho Chun,2007-02-02,Hong Kong
7010003,Poon,Cheuk Yin,2007-01-28,TST

 

To process the data

<?php
/**
* read the data from names.txt and output in table
*/

$fp = fopen('names.txt', 'r');       // open file for reading
echo  "<table align='center' cellpadding='2' cellspacing='1' border=0 style='background-color: #CCC'> ";
while ($data = fgetcsv($fp)) {       // read each line
    list ($bbcode, $family, $given, $dob, $place) = $data;
    echo "<tr><td colspan=2 style='text-align:center; font-weight:700'>$bbcode</td></tr>
            <tr><td>BB</td><td style='background-color: #EEE'>$family $given</td></tr>
            <tr><td>Date of birth</td><td style='background-color: #EEE'>$dob</td></tr>
            <tr><td>Name of mother</td><td style='background-color: #EEE'>Mrs $family</td></tr> 
            <tr><td>Name of father</td><td style='background-color: #EEE'>Mr $family</td></tr> 
            <tr><td>Place</td><td style='background-color: #EEE'>$place</td></tr> ";
}
echo "</table>" ;
?>

Thank you for your kindly reply  :D

 

The result was:

Warning: Wrong parameter count for fgetcsv() in /home/a0701baby/domains/0701baby.net/public_html/ifmain03.php on line 8

 

 

In my file, line 8 was:

while ($data = fgetcsv($fp)) {      // read each line

 

Would you like to solve this? Thanks again^^

According to my manual, only the first parameter is mandatory

 

array fgetcsv ( resource handle [, int length [, string delimiter [, string enclosure]]] )

 

 

But you can try providing all of them

 

while ($data = fgetcsv($fp, 1024, ',', '')) {

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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