PHP - Nesting Whiles In Ifs
I am writing a script that can handle either csv or xls files. Once the data is parsed for either type it goes into an array $data and all the same operations are done on it.
Right now, it is only set up to handle a csv file. This is the code I have right now: //Load the CSV files in the UPLOAD_PATH directory $fileList = $fs->loadFiles(UPLOAD_PATH, "csv"); // Loop through fileList array to processes each file foreach($fileList as $filename) { // Attempt to open the next file in the list and process it. if (($handle = fopen(UPLOAD_PATH.$filename, "r")) != FALSE) { // Create the input array while(($data = fgetcsv($handle, 0, ",")) != FALSE) { // ... do the magic How do I add another set of instructions to goto the parseXLS function that performs basically what fgetcsv does? I think in this case a try...catch statement would be too loose, and wouldnt be the best way. What I would LIKE to do is if (a csv file) { while(($data = fgetscv($handle, 0, ",")) != FALSE) { } else if (an xls file) { while ($data = parseXLS()) { } // Do the magic } // end of while I know this won't work but i need to do something along those lines. Any ideas? Similar Tutorialshi i have a db of tv shows, episodes and descriptions. they are in the db like: name | series | episode | description Friends | 1 | 1 | example Friends | 1 | 2 | example Scrubs | 1 | 1 | example I want to display it this way: Quote Friends Series 1 Episode 1 Series 1 Episode 2 Scrubs Series 1 Episode 1 cant seem to get it to fully work - it will display it for the first show but not do more than one thanks for any help I have a query that is very messy. I have tried using () to seperate everything and when I do it just all goes wrong. here is the query SELECT posts.*, users.*, countries.countryID, countries.country FROM posts INNER JOIN users ON users.userID = posts.postUserID INNER JOIN countries ON countries.countryID = users.userCountry WHERE posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'sightseeing' OR posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'Inner city sightseeing' OR posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'Outer city sightseeing' ORDER BY posts.postID DESC Is there a way of re-writing this so that it has brackets seperating everything and it will still work. It works for the minute but re- writing posts.cityID = '".$row_rs_city['cityID']."' inbetween every OR just seems wrong. Thanks for your help guys This code returns part but not all of what I'm asking it to do. Variable $av1 is drawn from the database and always has a value of 1 or 0 (tinyint). If value is 1 a green ON is displayed and if 0 a red OFF is displayed. That works. The variable $group1 exists or not, and if exists the code should proceed and if not exist there should be a display of "". In other words, I do not want to see any display if $group1 does not exist, but this is not working. There is always a display. Can anyone point out my mistake? Code: [Select] <?php if($group1 != "") { { echo "<input type='radio' name='1' value='$group1' />"; ?> <?php echo $group1; } ?> is switched <?php if($group1 != "") if($av1 !== 0) echo ("<span style=\"color:#080\">ON</span>"); else echo ("<span style=\"color:#F00\">OFF</span>"); }else{ echo ""; } ?> if($AClass->select_error || $BClass->ErrorFile){ include("/path/to/files/".($AClass->select_error ? 'no_selector' : 'error_file_missing').".php"); exit; } if($CClass->WriteError){ include("/path/to/files/FileWriteError.php"); exit; } Probably a simple one for you folks, but I can't for the life of me work out how to make the above into 1 line of ternarys. Cheers, Rw Hi all, I'm new to php and I was trying to send the results of one query to another query. I need the results of both queries. Here are the queries $cui1 = mysql_query("SELECT DISTINCT CUI FROM MRSTY WHERE STY='ABC' "); $cui2 = mysql_query("SELECT DISTINCT CUI FROM MRSTY WHERE STY='XYZ' "); I want to know if this is possible $cuis = mysql_query("SELECT CUI1,CUI2,REL FROM MRREL WHERE CUI1 IN $cui1 OR CUI1 IN $cui2 "); Thanks. I think this is the right place to post this but please move if needed as this is my first time on this site. You can also contact me through AIM: MadLittleMods@gmail.com Okay so I have a multidimensional array that is giving me an error when I put it in a function: ERROR: Warning: Invalid argument supplied for foreach().. line 123 LINE 123: foreach ( $affiliates as $affiliate ) This does work when in one file test2.php which is attached to this post... Here is my whole function: // affiliate content function start: function affiliates_content() { global $context, $settings, $options, $scripturl, $txt; foreach ( $affiliates as $affiliate ) { echo ' <table> <tr> <td> <a href="',$affiliate[href],'"><img src="',$affiliate[image],'"></a> </td> <td> ',$affiliate[title],' <br> <small><a href="',$affiliate[href],'">',$affiliate[href],'</a></small> </td> </tr> </table> <br> '; } } // affiliate content function end Here is my whole array which is located on another file (subs.php - smf) // affiliates array for affiliates page $affiliates = array( array( "title" => "aff_blah1", "image" => "http://aff_blah1.net/images/aff_blah1.png", "href" => "http://aff_blah1.net/" ), array( "title" => "aff_blah2", "image" => "http://aff_blah2.net/images/aff_blah2.png", "href" => "http://aff_blah2.net/" ), array( "title" => "aff_blah3", "image" => "http://aff_blah3.net/images/aff_blah3.png", "href" => "http://aff_blah3.net/" ), ); // affiliates array end Does anyone know how to fix this problem? I am working on a feed but am getting the nesting wrong, the </event> is appearing at the end of the whole feed and not at the end of the particular event. Here is a link to the feed http://www.horseeventsuk.com/events-feed-test.php and my code is Code: [Select] ?> <?php :'( $output_as_html=false; if ($output_as_html) echo '<html><head><title>Events Feed</title></head><body><h1>Events Feed</h1>'; $xml='<events>'; $sql="SELECT * FROM `events` WHERE event_status='live' ORDER BY event_created DESC LIMIT 3;"; $xml.='<event>'; $result=mysql_query($sql); if ($result) { while ($row=mysql_fetch_array($result)) { $event_id=$row['event_id']; $venue_id=$row['ven_id']; $county_id=FindVenueCountyId($venue_id); //print("Found event ".$event_id.'<br />'); $xml.=EchoRowItem($row,'title',$output_as_html); //$xml.=EchoRowItem($row,'description',$output_as_html,'event_details'); $xml.=EchoTag('contact_email','gilly@rackfield.co.uk',$output_as_html); $xml.=EchoTag('county',FindVenueCountyName($venue_id),$output_as_html); $xml.=EchoTag('postcode',FindVenuePostcode($venue_id),$output_as_html); $xml.=EchoTag('website_address',BuildEventURL($event_id,$county_id),$output_as_html); $xml.=EchoRowItem($row,'startdate',$output_as_html); $xml.=EchoTag('category','Animals, Convservation',$output_as_html); $xml.=EchoTag('category','Sport',$output_as_html); $xml.=EchoTag('category','Exhibitions, Shows',$output_as_html); } } $xml.='</event>'; $xml.='</events>'; if ($output_as_html) echo '</body></html>'; else { // serve the xml file header("Content-type: text/xml"); echo '<?xml version="1.0" encoding="UTF-8"?>'; echo $xml; } exit; I've been hacking at this for 3 hours now.. My syntax seems to be screwed up on something, but I cannot find it for the life of me! Error thrown is obvious I am missing or have something nested wrong! Parse error: parse error in C:\wamp\www\php\php_study\login_processor.php on line 22 Here is my code. Error is being thrown on the line of the elseif statment. <?php include('db_cxn.php'); $cxn = mysqli_connect($host, $user, $pass, $db) or die("Query died: connect"); $sql = "SELECT username FROM users WHERE username = '$_POST[username]'"; $result = mysqli_query($cxn, $sql) or die("Query Died: username"); $num = mysqli_num_rows($result); if ($num > 0) { $sql = "SELECT username FROM users WHERE username='$_POST[username]' AND password=md5('$_POST[password]')"; $result2 = mysqli_query($cxn, $sql) or die("Query died: password"); $num2 = mysqli_num_rows($result2); if ($num2 > 0) { $_SESSION['auth']; $_SESSION['logname'] = $_SESSION['username']; } //if ($num2 > 0) header("Location: the_secret_page.php"); } //if ($num > 0) else { $message1 = "The login name, '$_POST[username]' does not exist in the database. Please try again"; $username = strip_tags(trim($_POST[username])); include('login_form.php'); } //else elseif ($num == 0) { $message1 = "The username you entered does not exist. Please try again.s"; include("login_form.php"); } //elseif ($num == 0) ?> I don't think my form html/php code is necessary, but I'll include it for reference: <h1>Login Form</h1> <?php $fields_1 = array("username" => "User Name", "password" => "Password"); ?> <form method="post" action="login_processor.php"> <fieldset> <legend>Login Form</legend> <?php if (isset($message_1)) { echo "$message_1"; } //if (isset($message_1)) foreach ($fields_1 as $field => $value) { if (preg_match("/pass/i", $field)) $type = "password"; else $type = "text"; echo "<div id='field'> <label for='$field'>$value</label> <input id='$field' name='$field' type='$type' value='" . @$$field . "' size='20' maxlength='50' /> </div>\n"; } //foreach ($fields_1 as $field => $value) ?> <input type="submit" name="Button" value="Login" /> </fieldset> </form> |