kitman41 Posted April 21, 2007 Share Posted April 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/ Share on other sites More sharing options...
xenophobia Posted April 21, 2007 Share Posted April 21, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234542 Share on other sites More sharing options...
Barand Posted April 21, 2007 Share Posted April 21, 2007 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>" ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234599 Share on other sites More sharing options...
kitman41 Posted April 21, 2007 Author Share Posted April 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234648 Share on other sites More sharing options...
Barand Posted April 21, 2007 Share Posted April 21, 2007 Do you have this data stored anywhere? Database? Textfile? Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234651 Share on other sites More sharing options...
kitman41 Posted April 21, 2007 Author Share Posted April 21, 2007 NO, I have no text file or data stored. Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234779 Share on other sites More sharing options...
kitman41 Posted April 21, 2007 Author Share Posted April 21, 2007 Anyone can help me, please? Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234834 Share on other sites More sharing options...
Barand Posted April 21, 2007 Share Posted April 21, 2007 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>" ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234863 Share on other sites More sharing options...
kitman41 Posted April 22, 2007 Author Share Posted April 22, 2007 Thank you for your kindly reply 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^^ Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234966 Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 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, ',', '')) { Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234969 Share on other sites More sharing options...
kitman41 Posted April 22, 2007 Author Share Posted April 22, 2007 wow, Barand, Thank you very much! It's success! Hahaha! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/47988-solved-i-dont-know-how-to-fill-in-the-table/#findComment-234975 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.