
eche
New Members-
Posts
7 -
Joined
-
Last visited
Never
Profile Information
-
Gender
Not Telling
eche's Achievements

Newbie (1/5)
0
Reputation
-
how to check for key in multidimentional array and then assign it's values
eche replied to eche's topic in Applications
Hi, Thanks - that would work. How could I change that code so that I may use a foreach statement to assign the values to this new array from a flat file database? Something like?: $results = file() foreach ($results as $result) { $explode = explode("|",$result); $item['code'] = explode[0]; $item['name'] = explode[1]; $item['desc'] = $explode[2]; $item['status'] = $explode[3]; } foreach ($item as $key=>$value) { $array = array($item['code'][$key] => array(0 => $item['name'][$key], 1 => $item['desc'][$key], 2 => $item['status'][$key])); } $search = isset($_GET['code']) ? $_GET['code'] : 101; if(array_key_exists($search, $array)){ $name = $array[$search][0]; $desc = $array[$search][1]; $status = $array[$search][2]; }else{ echo "Array key does not exists"; } Thanks. -
Hi, Could never really get my head around arrays, so can someone please help me understand what I'm missing here. I have an array (size of it does not really matter - at best it will have around 20 elements). It's structure is ['code'] => (array [0] => 'name', [1] => 'description', [2] => 'status') so example: ['100'] => (array [0] => 'test', [1] => 'this is a test code', [2] => 'allowed') I want to be able to match code to the one that is being searched and if so, then assign name, description and status to variables for display. At the moment I have: $search = $_GET['code']; if (in_array($search,$array[$search])) { $name = $array[$search][0]; $desc = $array[$search][1]; $status = $array[$search][2]; } I get an error message of Warning: in_array() [function.in-array]: Wrong datatype for second argument in /var/... on line ... (the line corresponding to my in_array search) I'm obviously doing something wrong, but what is it? Thanks in advance :-\
-
sorting flat file data by date and displaying in table
eche replied to eche's topic in PHP Coding Help
Just also, I wanted to sort by task title with date remaining as the key. I had put an if statement where I put the date range code as above however this produced a table with the shading of every second row incorrectly (where some rows were shaded 3/4 in a row, then not shaded for several rows etc.) Then I realised I probably should have put the if statement before it got to this step where the data is actually put into the array. So I put it in after this line: $explode = explode(",",$line); I put: if ($explode[1] == $search_task) { ..explode data from lines into the array } where $search_task was the task name I was looking for. So that section of code now looks like: foreach($filedata as $line) { $explode = explode(",",$line); if ($explode[1] == $search_task) { $data_arr['date'][] = trim($explode[0]); $data_arr['task'][] = trim($explode[1]); .... etc } Short of using a database, this is what I think is as good as it gets. Thanks -
sorting flat file data by date and displaying in table
eche replied to eche's topic in PHP Coding Help
Thanks, that was probably the easiest way to do it and it worked. I changed dates to that format and I modified my code to be: <table cellpadding="5"> <tr class="trheading"> <td>Date</td> <td>Task</td> <td>Contact</td> </tr> <?php $filedata = file("data.txt"); $data_arr = array(); natsort($filedata); foreach($filedata as $line) { $explode = explode(",",$line); $data_arr['date'][] = trim($explode[0]); $data_arr['trimester'][] = trim($explode[1]); $data_arr['task'][] = trim($explode[2]); $data_arr['information'][] = trim($explode[3]); $data_arr['link'][] = trim($explode[4]); $data_arr['contact'][] = trim($explode[5]); $data_arr['email'][] = trim($explode[6]); $data_arr['phone'][] = trim($explode[7]); } foreach($data_arr['date'] as $key=>$value){ if ($key % 2) { echo '<tr class="tralt">'; } else { echo '<tr>'; } echo "<td>".$data_arr['date'][$key]."</td>\n"; echo "<td><strong>".$data_arr['task'][$key]."</strong><br />\n"; echo $data_arr['information'][$key]." <a href=\"" .$data_arr['link'][$key]. "\">" .$data_arr['link'][$key]. "</a></td>\n"; echo "<td>".$data_arr['contact'][$key]." " .$data_arr['phone'][$key]. "<br /><a href=\"".$data_arr['email'][$key]. "\">".$data_arr['email'][$key]."</a></td>\n"; echo "</tr>\n"; } ?> </table> I was then able to only display entries I wanted by adding: if (($value < $start_date) || ($value > $end_date )) {} else { between: foreach($data_arr['date'] as $key=>$value){ and: if ($key % 2) { where $start_date and $end_date are dates set in the same format 'yyyymmdd' Thanks for your suggestions too meltingpoint -
Hi, I'm probably missing something really obvious here but can you please help. What I have is a comma separated flat file which contains: date,task name,information,url link,contact name,contact email,contact phone # 10/10/10,example,this is an example,http://www.example.com,John Doe,[email protected],12345678 and so on. Data is not sorted. (Have to use a flat file as no access to DB) What I am attempting to do is extract the data from the file, sort by date (where date is dd/mm/yy) and display in a table. What I then hope to do later is show only selected date ranges (e.g by month or a year view from today's date + 12 months) Here is what I've got right now which at the moment is extracting the date and showing things in a table the way I need them: <table cellpadding="5"> <tr class="trheading"> <td>Date</td> <td>Task</td> <td>Contact</td> </tr> <?php $filedata = file("data.txt"); $data_arr = array(); foreach($filedata as $line) { $explode = explode(",",$line); $data_arr['date'][] = trim($explode[0]); $data_arr['trimester'][] = trim($explode[1]); $data_arr['task'][] = trim($explode[2]); $data_arr['information'][] = trim($explode[3]); $data_arr['link'][] = trim($explode[4]); $data_arr['contact'][] = trim($explode[5]); $data_arr['email'][] = trim($explode[6]); $data_arr['phone'][] = trim($explode[7]); } foreach($data_arr['date'] as $key=>$value){ if ($key % 2) { echo '<tr class="tralt">'; } else { echo '<tr>'; } echo "<td>".$data_arr['date'][$key]."</td>\n"; echo "<td><strong>".$data_arr['task'][$key]."</strong><br />\n"; echo $data_arr['information'][$key]." <a href=\"" .$data_arr['link'][$key]. "\">" .$data_arr['link'][$key]. "</a></td>\n"; echo "<td>".$data_arr['contact'][$key]." " .$data_arr['phone'][$key]. "<br /><a href=\"".$data_arr['email'][$key]. "\">".$data_arr['email'][$key]."</a></td>\n"; echo "</tr>\n"; } ?> </table> I tried using usort function which was comparing dates however this messed up the data and I ended up with the wrong dates being applied to the wrong data. Sort function works only by sorting the first numbers (in this case day) in ascending order which doesn't help because months and years are out of whack. Must be missing something - have I over complicated my use of arrays - what am I missing here? :-\
-
Thanks for your responses so far. I have had a look at sessions, and it's probably the way to go. As for storing values and passing them through hidden fields, I did try this, but I tried it coupled with a switch and that's where things went pear shaped. Let me try and explain my form. Here's a link to a flow chart (quickly knocked up, not really an artist) to better explain the process. At least I think it does. http://i44.tinypic.com/2ywshs4.jpg That is the way I would like to work. So for it to happen that way, I need to know what step they are up to plus whether they have filled it in (validation side). This is what I have had thus far (excuse the syntax): if !isset(submit) { show first step } else { if step = 2 { show step 2 } elseif step = 3 { show step 3 ... etc ... } else { show step 1 with error } Showing a particular step, I have a switch/case system. So step 2 corresponds to the case 2 for example. Now the problem with all of that is the validation. How or where do I fit the validation. Or am I going about this the wrong way with that type of system I have there?
-
Yes I know what you're thinking, not another really basic question that you can answer. But I'll be honest with you, I did try searching and the results are less than what I wanted. So let me try and explain what I have done and what I am trying to do! Here is what is involved: 1) The form is a multi step "contact us" process, so depending on the option, something else displays and so on. 2) Validation on the server-side, I do not want to use javascript as it presents problems (turned off on the client side - I know, highly unlikely, but you never know) 3) One the user gets through the certain steps, anywhere from 3-5 steps, they are presented a form which they fill out (personal details, questions etc - being a contact us form) and then submit. Processing of this data is no issue, it is sent to a specific email address - simple. Now what I have done so far is create the individual "pages" if you like, every possible option that a user can be presented with. I have also worked out the validation for each, separately they work like a charm, putting it all together is an absolute nightmare. What I want it to do is to work off one page or file. So if I had the address http://www.domain.com/contact.php .. as the user is working through the steps, they are submitting to the same file. I don't want (the easier option, I know) individual files, such as step1, step2, or red, blue, green etc. So when it came time for me to put things together, I ended up with a long list of if, else if statements that in the end simply did not work. I tried using a switch/case setup, only issue with this was the validations were not working. I looked at sessions, but fell short of understanding how to display a certain step without employing an if statement or 3. Had this have been dos, I would have probably used goto, it's not an option here however. I also know there is a solution using classes, I search in here and google and found it. But I would prefer a "home-made" solution. So my question is what am I missing here (and please, don't say everything, that's just as helpful as saying f* off). I want it to work the way I intended, a multistep contact form, no javascript, server side validations and a submission at the end to an email. It sounds simple. Did I mention I've pulled some of my hair out? ??? The funny thing is I will probably look back on this in a couple of months time and realize what an idiot I really am.