Jump to content
Larry Ullman's Book Forums

Cofa

Members
  • Posts

    54
  • Joined

  • Last visited

Everything posted by Cofa

  1. Thank you HartleySan, for offering to help! The link is working well on browsers on my computers - but I do notice that sometimes I need to use refresh in order to load the page. I don't know if this is my browser's problem or problem of the file. For the purposes of this topic, I have provided the file in both php and txt below for your review, from these links: http://www.cofatsui.com/h5/00-h5banner1x1.php http://www.cofatsui.com/h5/00-h5banner1x1.txt Below will explain what I wanted to achieve: a. The page from the above link will open some banner ads from third parties, one ad at a time. The banner ads are served by a URL in an iFrame embedded in the above page (we say that this page is in the "parent window"). b. Clicking on the ad image will open the ad's target website, EITHER in a new window, OR in the parent window, depending on the target settings of the individual banners. c. For the sake of consistence, I want the target website of all the banner ads to open in the parent window only, irrelevant to the target settings of individual banners. In other word, even if a banner has a target setting to open its target website in a new window, the proposed JavaScript will still open the target website in the parent window where the iFrame is held - hence I use the word FORCE here. Is such JavaScript possible? And how? Thanks...
  2. Hello, I know this question is outside PHP but I could not find answers with my brief research - I will withdraw this if necessary My site has an iFrame that would display banner ads from third parties. Those banner ads will open EITHER in a new window OR in the parent window (parent window = the window holding the iFrame), depending on the banner files - the URL and link target are embedded in the Flash banners. My sample page with iFrame: http://www.cofatsui.com/h5/00-h5banner1x1.php I wonder what the JavaScript code would be like that would force all banner links to open in *only* the parent window, for consistence sake. (Opening in *only* a named new window is also okay, as long as the target is consistent for all banners.) I know PHP cannot do this. I do not know JavaScript coding so any help, or any direction to other JavaScript forums, would be appreciated. Thanks! Cofa
  3. Thanks to HartleySan and Larry for the further explanation and confirmation. So it would in turn also suggest that putting the session_start() in the header file is a better practice (as per HartleySan's first message).
  4. Hello Hartley San, Thank you for the advice - this shall clear my confusion for sure. I think I'll stick with putting the session start in the BODY file (position "B") as this file will always run first. But I do have a question about putting the session start in the HEADER file (position "A"). If there are some scripts run in the beginning of the BODY file before it calls the HEADER file, would that session start function in the HEADER file still work properly - as it is called after some scripts have been executed?
  5. Hello all, On my website I use a "body template" to hold contents of various pages, and the body template files will call for a "header template" to display the header contents of the site, like in the following two template files: Note: the "body.php" file runs first. HEADER TEMPLATE FILE 'HEADER.php': ================================ <?php session_start(); //POSITION "A" if (...) { ... } else { ... } ?> <!doctype html> etc etc </head> <body> <!-- END HEADER TEMPLATE FILE --> BODY TEMPLATE FILE 'body.php': ================================ <?php session_start(); //POSITION "B" if (...) { ... } else { ... } include('HEADER.php'); ?> <!-- BEGIN BODY CONTENTS --> Body contents etc etc <!-- END BODY CONTENTS --> <?php include('FOOTER.php'); ?> <!-- END BODY TEMPLATE FILE --> And as you can see, I also use session on the site as well. I learned that session_start() must be placed in the beginning of a file, so currently I have session_start() in both the body.php and HEADER.php files (see positions "A" and "B" in the templates above). Currently the pages are displaying fine but somehow I think I might have missed something. Could someone please let me know if the above use is correct? Or, should I use only one session_start() in one of the two templates? And, which one should I keep (position "A" or "B")? Thanks in advance! Cofa
  6. No, I cannot. But Larry, I have to thank you for reminding me! I have reviewed my testing process and realized that my script (in my previous post) does just fine! The script can really re-direct reader to another site called for by the ad (link), and at the same time set the session. (I must have neglected something in my previous test.) For those who are interested, please view the working files from this link: http://www.imahjong.com/test/09_multitask.php Cheers! Cofa 6/6/2012
  7. Thank you Larry! Glad to know that my belief is correct - "PHP can do this!" For the JavaScript part, I'll have to ask my web host to develop the script. For academic purposes of this forum please allow me to continue with the query. I have tried a "possible solution" but it doesn't seem to work: Please refer to "PAGE 1" of the SAMPLE SCRIPTS at the end - Firstly, an image ad shall have a "built-in link" that contains the following two items: "memberid" = the referrer ID of the advertiser "newsite" = the new website the image ad is supposed to call The "built-in link" of the ad will point to a specific page on the same site displaying the ad - see "PAGE 2". The "specific page" (Page 2) contains a script to do the multi-tasks that I expect to be done: The script first checks if a memberid is presented, if so it will set a session on the site. At the same time, the script will re-direct the reader to a "new site" called by the ad. The results? The script (on Page 2) did work, but only partially. It did re-direct to the "newsite" page, but the expected session was not set. Can someone point out what's wrong with the process please? Cofa 5/31/2012 = = = = = = = = = SAMPLE SCRIPTS = = = = = = = = = PAGE 1 (/page_1.php - or can be any other page): Link on the page or link built-in in an ad image displayed on the page =================================================== http://www.sitename.com/page_2.php?memberid=cofa&newsite=http://www.newsite.com/ PAGE 2 (/page_2.php): A specific page on the same site to execute the php script from Page 1 =================================================== <?php session_start(); if (isset($_REQUEST['memberid'])) { $_SESSION['memberid'] = $_REQUEST['memberid']; } if ($_REQUEST['newsite'] != '') { $reloadURL = $_REQUEST['newsite']; header("Location: $reloadURL"); exit(); } else { $reloadURL = $_SERVER['HTTP_REFERER']; header("Location: $reloadURL"); exit(); } ?>
  8. Hello all, I wonder how many individual tasks can be done and on how many sites via just a single php script (i.e., a URL link in php scripting). Let me explain my ideas... Say, the following link (a single php file) will open the homepage of this forum site: http://www.larryullman.com/forums/index.php The above php file can be in the form of a text link (like the one above), or built-in in a Flash image (like in any banner ads). Clicking on the link (or a banner ad) will open a new webpage the script is designed to do. Now my question is whether the tasks #4 and #5 of the following process can BOTH be achieved at the same time (triggered by one click): 1. A php link is built-in in an image ad; 2. The ad is served to a participating website (Site 01) - that is, Site 01 now displays the ad on its pages; 3. A visitor to Site 01 clicks on the ad; 4. The script opens a new page on another site (say, Site 02); 5. A Session is created on Site 01, allowing "membership information of the image ad" to stay with Site 01 for a predefined period. In other word, when a visitor clicks on an image ad on Site 01, the visitor will be directed to a new webpage on Site 02, and at the same time, the "click" will also create a session on Site 01 for that visitor, so that if the visitor later comes back to Site 01 and make a purchase, referral fee will be credited to the member of the image ad. Any comment please! Thanks, Cofa
  9. Hi everyone, I refer to my message in my other post some time ago: "Currently I am playing with accessing database of a remote database server from another website - so far things are in progress! (It was a complicated process in the beginning - I will share my experience sometime when my time is more flexible. Reference in book: Page 351, Tips item #2.)" Page 351, Tips item #2 in Larry's book says: The localhost value is used as the hose name when both the PHP script and the MySQL database reside on the same computer. You can use PHP to connect to a MySQL database running on a remote server by changing the host name in the PHP script and creating the proper permissions in MySQL. Note that in fact "and creating the proper permissions in MySQL" is a critical part in a setup process - and the permissions have to be obtained from the server host of the remote MySQL database. In my case, I have accounts of two sites that are hosted by two different companies. I wanted to set up a database on one site but wanted it to be accessible from scripts on both sites. Doing whatever modifications with the scripts on the site that does not hold the database will not make the scripts work with the remote database. Permissions must be obtained from the hosting company who hosts the database server before the scripts on the remote site to be workable. The "permission" is some configuration the host has to adjust on their server, and has nothing to do with the scripts (other than the access info as mentioned in the book). Hope this info is helpful to other 'newbies' like me Have a great day! Cofa 4/2/2012
  10. Thank you for replying; but I am confused... Your "no" - does it mean I cannot use one database for different projects? But your "but..." confuses me. Say I have a project for a "tour interest group" that may require 3 tables, and I have another project that deals with my "business clients management" that requires 10 tables. Can I create all of these tables in just one database, while tables for the "tour interest group" will have names prefixed with "tour-"; and tables for "business clients management" will have names prefixed with "buz-"? Or must I use two databases to hold the two groups of tables separately? Regards, Cofa 2/27/2012
  11. My web host allows 3 databases for my host account and I understand I can create as many as possible tables per database (except for any limitation with MySQL that I don't know). My question is: Can I create different *groups* of tables in just one database, with each *group* being for an individual project that is not related to other projects? Thanks, Cofa 2/27/2012
  12. Hello all, I have been into learning and "playing with" some simple coding with php and mysql recently. Currently I am playing with accessing database of a remote database server from another website - so far things are in progress! (It was a complicated process in the beginning - I will share my experience sometime when my time is more flexible. Reference in book: Page 351, Tips item #2.) There is one question I wonder if you could help me clarify: Does mysql_close() a *must step* in a script? Currently in my few scripts in my studies (I started just recently), I noticed I do not have this closing command in my scripts but the purposes of the scripts seemed to be fulfiled well. But I am curious if this "bad habit" could cause damage or security risk to the database host server (normally on a remote site), or to the server of the calling site (a website outside the database host site). [Well, I do own all these sites that are hosted by different hosting companies.] Thanks, Cofa 2/26/2012
  13. Hi Larry, Thank you very much! A good lesson about using htmlentities() and stripslashes(). (I believe my system host has the "Magic Quotes" turned on so the above are my only solutions.) Have a great weekend! Cofa 1/13/2012
  14. Thank you Larry, I used echo htmlentities($packcontents) and the problems in my initial post have gone. And I click on the button "Click to Launch the Order Entry Form" to generate the new page... I now write my code for the new page below: <?php //BLOCK 1 - ORDER ENTRY FORM if ($_POST['pageid']=='page11') { //Display Order Form $packname = $_POST['packname']; $packcontents = $_POST['packcontents']; $packprice = $_POST['packprice']; $packdate = $_POST['packdate']; ?> <h1 style="text-align:center; font-size:18px;">Tour Order Form - <?php echo $packname; ?></h1> <center style="width:90%; margin:18px auto; padding:8px; border:solid 1px #bfbfbf;"> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td align="right" valign="top" style="padding:6px;"><nobr><b>Tour Name:</b></nobr></td> <td align="left" valign="top" style="padding:6px 12px;"><?php echo $packname; ?>.</td> </tr> <tr> <td align="right" valign="top" style="padding:6px;"><nobr><b>Package Summary:</b></nobr></td> <td align="left" valign="top" style="padding:6px 12px;"><?php echo $packcontents; ?></td> </tr> <!--SNIPPED--> </table> </center> <?php //END BLOCK 1 - ORDER ENTRY FORM } else { //CODE FOR ANOTHER NEW PAGE SNIPPED END OF QUOTE In the new page, the following texts are displayed - from <?php echo $packcontents; ?>: (1) Direct flight to Beijing by \"Boeing 747\". (2) 3 nights at Beijing Hotel http://www.beijinghotel.cmm. Also in the source code of the link, it shows: <a href=\"http://www.beijinghotel.cmm/\" target=\"_blank\">http://www.beijinghotel.cmm</a> As you can see, the webpage and the source code both print \" instead of just " - the latter would even make the link (URL) go to a "not found" page. Could you please show me how to get around this problem? Thanks, Cofa 1/11/2012
  15. Hello all, I have problem about quoting texts using variable in the input tag of a form mail... Please allow me to quote my script first: <!-- File "00_test_htmldecode2.php" --> <center style="width:60%; margin:60px; padding:12px; border:solid 1px #bfbfbf;"> <?php //DEFINE VARIABLES HERE $packname = 'Beijing 10 days tour'; $packcontents = '<b>(1)</b> Direct flight to Beijing by "Boeing 747". <b>(2)</b> 3 nights at Beijing Hotel <a href="http://www.beijinghotel.cmm/" target="_blank">http://www.beijinghotel.cmm</a>.'; $packprice = 2000; $packdate = 'June 15, 2012'; ?> <table cellpadding="0" cellspacing="0" border="0" style="margin:12px;"> <form name="form" method="POST" action="/00_test_htmldecode.php"> <input type="hidden" name="pageid" value="page11"> <input type="hidden" name="packname" value="<?php echo $packname; ?>"> <input type="hidden" name="packcontents" value="<?php echo $packcontents; ?>"> <input type="hidden" name="packprice" value="<?php echo $packprice; ?>"> <input type="hidden" name="packdate" value="<?php echo $packdate; ?>"> <tr> <td align="center" valign="middle" style="padding:8px;"> <input type="submit" name="submit" value=" Click to Launch the Order Entry Form "> </td> </tr> </form> </table> </center> END QUOTE From the above code I was supposed to see only a "submit button" in an enclosed square. However, the following texts were also, but unexpectedly, displayed before the button (copied from the screen): (2) 3 nights at Beijing Hotel http://www.beijinghotel.cmm."> After some review I believe the problem is from the input tag of: <input type="hidden" name="packcontents" value="<?php echo $packcontents; ?>"> Also as a matter of fact, when the above form was submitted to a new page, only texts "(1) Direct flight to Beijing by" were displayed. My guess is that when the variable "$packcontents" was printed to the value of the input tag, the quotes in the contents of the variable have caused problems in the html rendering. Could anyone please show me how to echo a variable in the input tag that could avoid the above problems? Thanks, Cofa 1/9/2012
  16. Hi Josee and Abigail, The script does work as I expected - thanks again for all the advice! And I certainly will proceed to start learning MySQL soon. Cheers! Cofa 10/27/2011
  17. Hi Josee: Thanks for your clarification. No worry as I just wanted to see if I could learn any improvement to my writing skill. As to Abigail's comments, I want to let you know how I would use my script. Firstly, the data file is totally prepared internally - I gather data of subscribers from other means (form mail or fax etc) and save them in an Excel file. As you already pointed out, my Excel file will have all fieldnames on the first row and it can be easily updated on my computer. Once done, the file will be saved in csv and uploaded to the server. I do not plan to gather data from writing to file at this point (my server host told me the security risk is high). Secondly as to ussage, the script is for members of our club to inform their contact about any upcoming event, in their own names. For example, if our club has an upcoming Christmas party, I can prepare an online registration form. Any group member of us, say one called Subscriber A, can then send a URL containing his unique "scode", something like http://ourupcomingparty.cmm/?matcher=aa11 (a fake URL), to his mailing list. The URL will go to the online registration form, with some id data imprinted in some <input> tags of the form. So when we receive the registration request, we will know who has introduced this guest (say, as per our text file, scode "aa11" is from Peter KWAN). With this script, the online form can also display name or id info of the member who sends the form. Of course the script can be used in other similar situations. For me, the good part is I don't need to worry about the order (positions) of the fieldnames in my Excel files. I am not using any database yet (will learn it next step), so text file is the only way I can go with (or, is it?). And because of the required purposes, I had to research to develop this script. Cofa 10/26/2011
  18. Hi Josee, I read about the sprintf() function you provided and my understanding is that it might be good for printing results; I can't see any use for my primary purposes (i.e., to match a search from a text file). Maybe you could please point out something that I've missed but thanks for commenting anyway. Hi Abigail, the use of "scode" (secret code or whatever) could serve as a general term or can be a better control of the contents of a file - I expect that it shall be issued by the host to maintain uniqueness ("firstname" could have more than one identical value in the file). I found "array_search()" via Google search - I am very happy with this function and indeed it plays a very important part of the script. (I thought of using "explode" at first but was stuck at being unable to determine the exact position of the elements of Row 1.) By the way, I assume advanced programmers won't use text files for the purposes of database so the above script could be for newbies like me only? Cheers everyone! Cofa 10/25/2011
  19. Hello Abigail, Thank you for your direction! With some research, I now have come up with a working script (tested and shown below). I also enclosed a revised text file. Note that I have added a new field ("scode") to the file. With the new script, I will have to check if the secrete code ("scode") of a record matches the given $matcher. If it does, elements of the record are printed. I believe the new script will work with any data file without worrying about the positions of the fieldnames, as long as the fieldnames in all files are consistant. I am very happy with the results! Again, thanks a lot to Abigail! Cofa 10/24/2011 PS: I am still a newbie. So please feel free to show me any improvement that can be made with the script. Text file: =================================================== firstname#lastname#scode#companyname#intro Peter#KWAN#aa11#First Street Company#This is my introduction. Please send me your comments. Thanks! Mary#THOMSON#bb22#Johnson Elementary School#I am a grade 3 teacher here. I love facing the kids everyday! Billy#YU#cc33#Billy Travel Agency#We specialize in cruise ship tours in the Caribbean - Let me know when you'd like to have your vacation! Peter2#KWAN2#dd44#First Street Company#This is my introduction. Please send me your comments. Thanks! The revised script: =================================================== <div style="margin:60px;"> <?php $matcher = $_REQUEST['matcher']; $file = "00_test_readcsv_records.csv"; $fp = fopen($file, r); //Finding the position of fieldnames from Row 1 $fieldnames = fgetcsv($fp, 1000, '#'); $scodeindex = array_search("scode", $fieldnames); $firstnameindex = array_search("firstname", $fieldnames); $lastnameindex = array_search("lastname", $fieldnames); $companynameindex = array_search("companyname", $fieldnames); $introindex = array_search("intro", $fieldnames); echo "<p><b>Fieldnames of Row 1 have been read and identified:</b> <br>Scodeindex = " .$scodeindex. "<br>Firstnameindex = " .$firstnameindex. "<br>Lastnameindex = " .$lastnameindex. "<br>Companynameindex = " .$companynameindex. "<br>Introindex = " .$introindex. "<br>==END OF READING Fieldnames on ROW 1==</p>"; //Finding elements of the matched record while ($row = fgetcsv($fp, 1000, '#')) { if ( strtolower($row[$scodeindex]) == strtolower($matcher) ) { $firstname = $row[$firstnameindex]; $lastname = $row[$lastnameindex]; $companyname = $row[$companynameindex]; $intro = $row[$introindex]; break; } } fclose($fp); ?> <p>PRINTING ELEMENTS OF THE MATCHED RECORD</p> <?php echo "<br>First name = " . $firstname; echo "<br>Last name = " . $lastname; echo "<br>Company name = " . $companyname; echo "<br>Introduction = " . $intro; ?> </div>
  20. Hello Abigail, Thanks for trying to help. May be let me redo my questions... First of all, let me simplify my data file. I am not sure if it is important if it is an Excel file; but it has to be a text file with a "value separator" - here I have used "#" as the separator. So here is the simplified file: File name: "00_test_readcsv_records.csv" ============================= firstname#lastname#companyname#intro Peter#KWAN#First Street Company#This is my introduction. Please send me your comments. Thanks! Mary#THOMSON#Johnson High School#I am a grade 3 teacher here. I love facing the kids everyday! As you can see, the file has 3 rows. Row 1 contains "fieldnames" only, while rows 2 & 3 represent two records. The purpose of row 1 is to identify the fieldnames of values in all other rows. With my previous script, it will read the file row by row, and check if the first element of a row ($row[0]) would match the value of "$matcher". If it does, the values of the row will be printed. With this script, it is always the first element ($row[0]) of a row to be checked. And the first element is corresponding to the fieldname "firstname" on the first row of the file. In my expectation, in some other files with which the existing script is to be used, the order of the fieldnames in the first row may be different. For example, in a particular file, the field "firstname" could be in the 3rd field (the 3rd element of every row, or $row[2]). If this is the case, the existing script will have to change accordingly or it will not work as expected. So my question, and my desire as well , is how I could change my script so that the specific element to be checked can be quoted by its fieldname (say, the field "firstname" in this example), regardless of the element's position in the row. To further explain my question, is there any similar expression like the first one below (match conditional), where an element in a row can be quoted by name corresponding to the fieldname? Say, having: if ( strtolower($row['firstname']) == strtolower($matcher) ) { //Quoting an element by name corresponding to its fieldname $row['firstname'] Instead of: if ( strtolower($row[0]) == strtolower($matcher) ) { //Quoting an element by its position in the row $row[0] Thanks, Cofa 10/23/2011
  21. Hello all again! This post is somewhat related to my previous question: "How To Identify A Record In Text File, Then Print Selected Values Of That Record?" I am still working with the same script but with a new problem: How could I read elements in csv file by the field names? To better illustrate my question, I list the following two parts of my work: Data file to be read, in "csv" format: Note the first line contains the field names like you make it up in Excel. File name: "00_test_readcsv_records.csv" ============================= firstname#lastname#companyname#intro 321#Lastname321#321 Company#Introduction 321 Peter#KWAN#First Street Company#This is my introduction. Please send me your comments. Thanks! Mary#THOMSON#Johnson High School#I am a grade 3 teacher here. I love facing the kids everyday! Billy#YU#Billy Travel Agency#We specialize in cruise ship tours in the Caribbean - Let me know when you'd like to have your vacation! 111How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. Peter2#KWAN2#First Street Company#This is my introduction. Please send me your comments. Thanks! PHP script (including formatting code): ================================================= <div style="width:780px; margin:30px;"> <p>1ST PART - INTERNAL CODE:</p> <?php $matcher = $_REQUEST['matcher']; $file = "00_test_readcsv_records.csv"; $fp = fopen($file, r); while ($row = fgetcsv($fp, 1000, '#')) { if ( strtolower($row[0]) == strtolower($matcher) ) { $firstname = $row[0]; $lastname = $row[1]; $companyname = $row[2]; $intro = $row[3]; break; } } fclose($fp); ?> <p>2ND PART - PRINT ELEMENTS OF MATCHED RECORD</p> <?php echo "<br>First name = " . $firstname; echo "<br>Last name = " . $lastname; echo "<br>Company name = " . $companyname; echo "<br>Introduction = " . $intro; ?> </div> ====================================END OF SCRIPT Now my question is, with the line if ( strtolower($row[0]) == strtolower($matcher) ) { Can I read elements of the array ($row) by a specific "field name" of the csv file? For example, if the "$matcher" is meant to match a value of the field "lastname" (not necessary to be the first element "$row[0]", but an element that can be identified by the "field name", not by its position in the array), how could I modify the above code to fulfil this job? The reason I want to do this way is whenever I need to change the order of the fields in the csv file, I can still use the same PHP script without changing the key# in the script. Thanks! Cofa 10/20/2011
  22. Thanks to Larry and abigail for the help. I now understood the logic. By the way, also thanks to Larry for the suggestions on the delimiter. Have a nice weekend! Cofa 9/9/2011
  23. Hi Larry, Yes, it works perfectly after removing the "fgets() while loop" from my code. But I just don't understand (may be I miss something). With my not-so-perfect code: while ($row = fgets($fp)) { while ($row = fgetcsv($fp, 2048, '#')) { if ($row[0] == $matcher) { How come the 2nd (or internal) loop could not read the first line of the text file from the first while loop? Or how come it (the 2nd loop) could not read the first line of the text file if it (the 2nd loop) works on its own? Hope this won't take up too much of your time Cofa 9/7/2011
×
×
  • Create New...