Monday 15 February 2010

php - Returning a variable that has to be updated from a function, not returning? -


There are so many codes, but most of them are irrelevant, so I will only post one snippet

  $ error_message = ""; The function died ($ error) // If there is something, send the URL with the error message {session_start (); $ _SESSION ['Error'] = $ error; Header ("Location: http://mydomain.com/post/error.php"); Die (); }   

This works fine, sends the user with an error session, which displays an error at error.php

  function fetch_post ( $ Url, $ Error_message) {$ sql = "SELECT * FROM inserted_posts WHERE name = '$ name'"; $ Result = mysqli_query ($ conn, $ sql); $ Num_rows = mysqli_num_rows ($ result); If ($ num_rows & gt; 0) {$ error_message = $ url} "Already exists in database, not added"; Return $ error_message; }}   

This also works fine, checks that if the "post" exists in the database, if this happens, then this error $ error_message

  while ($ current <= $ s) {$ dom = file_get_html ($ start_url. Current); // page + page number $ post = $ dom- & gt; Search ('div [class = post] h2 a'); $ I = 0; Where ($ i & lt; 8) {if (empty ($ posts [$ i])) // If this link is found in $ post_now = 'http://www.somedomain.org' then check $ post [$ I] - & gt; Href; // Extension and save it fetch_post ($ post_now, and $ error_message); // send to function} $ i ++; } $ Current ++; // one page in the current page number)   

This is the main loop, it leaves me some variables, and fetches posts from an exsternal website and in the function of the URL and error message Fetch_posts

(I send it together, and I'm referring it to the context couse i asume is the only way to keep it global?)

 < Code> if (strlen ($ error_message> 0)) {dead ($ error_message); }   

And this is the last snippet right after the loop, this error message is to send in a function error if the error message contains any characters, but it does not detect any characters is ?

you want:

  strlen ($ error_message)> ; <  

not

  strlen ($ error_message> 0)   

In addition, call-time pass After-5.3.0 the conclusion has been removed and removed after 5.4.0, instead of calling your function in this way:

  fetch_post ($ Post_now, and $ error_message);   

You might want to define it like this:

  fetch_post ($ url, and $ error_message) function {$ sql = "SELECT * FROM Inserted_posts WHERE name = '$ name' "; $ Result = mysqli_query ($ conn, $ sql); $ Num_rows = mysqli_num_rows ($ result); If ($ num_rows & gt; 0) {$ error_message = $ url} "Already exists in database, not added"; Return $ error_message; }}   

Although you are returning an error message in a loop, it would be better to do this:

  $ error_messages = array (); // ... while while loop ($ error = fetch_post ($ post_now)) {$ error_messages [] = $ error; } // ... is the end while the (! Empty ($ error_messages)) {dead ($ error_messages); // Make changes to work with your array}    

No comments:

Post a Comment