Jump to content

codebyren

Members
  • Posts

    156
  • Joined

  • Last visited

Everything posted by codebyren

  1. There are many ways to do this (and probably more) as you've just discovered: 1. using in_array() after exploding by comma 2. strcmp after exploding by comma 3. array comparison after exploding by comma and then by pipe I suspect that both the first two are faster than the third. But I do recall reading another thread of yours where you suggested calculating how close the user's balls were to jackpot - in which case having each chosen ball as an array value would be useful. Also, unless this is for a ridiculously large lottery, I doubt you would notice the performance difference.
  2. You can compare arrays with comparison operators which would take the order into account: <?php $numbers_chosen = "8|18|3,18|3|8,3|8|18"; $winning_numbers = "8|18|3"; // Let's get those winning numbers into an array $winning_numbers = explode('|', $winning_numbers); $winner = false; $tickets = explode(',', $numbers_chosen); foreach ($tickets as $ticket) { $balls = explode('|', $ticket); if ($balls == $winning_numbers) $winner = true; # This is where we compare the arrays (including order) } echo ($winner) ? "We have a winner" : "Try again."; exit; ?>
  3. The problem here is that the $organised_data variable needs to be an array of associative arrays (where each associative array represents a row of data to be updated in your database). At the moment, you have organised the submitted data in such a way that there is no relationship between values. You have an array that contains: 1. An array of pickupids 2. An array of pickuplocations 3. An array of Date1s 4. An array of Date2s 5. An array of Date3s When what you need is an array of arrays that look something like: <?php // I'm assuming the pickupid shouldn't be included in the data to be updated (since you used it as your WHERE clause) $single_row = array( 'pickuplocation' => 'Something', 'Date1' => 'some date', 'Date2' => 'another date', 'Date3' => 'different date' ); ?> This is also why I suggested using the pickupid (or whatever your table's primary key is) as your array keys in your HTML form. This would make it easier to manage the submitted data and maintain relationships between the data for updating.
  4. Well, the URL parameters are being split by the presence of "&" in the URL. So, it's breaking at where &cid=130 kicks in and adding "cid" as a new URL parameter (same with &utm_source etc.). If you need to pass this kinda info in the URL, you will need to look at http://php.net/manual/en/function.urlencode.php and its urldecode counterpart.
  5. I'd say learning to use SimpleXML is quite valuable long-term but there's nothing stopping you from using it alongside you current library of choice. With your sample XML: <?php $xml = '<Result><Info><Details><Type><Who><Name key="1">This is Me</Name><Number1 key="2">18.93</Number1><Number2 key="2">20.01</Number2><Number3 key="2">15.21</Number3></Who></Type></Details></Info></Result>'; $xml = simplexml_load_string($xml); // This is not ideal for seeing attributes but gives you a good idea of the XML tree/structure // print_r($xml); // Access the first <Name> in the XML echo "The first instance of name is: ", $xml->Info->Details->Type->Who->Name[0], "<br>"; // (notice how Info->Details->Type->Who->Name matches the XML tree/structure making it pretty easy to navigate) // Access the attributes tied to the first instance of <Name> using atributes() method foreach($xml->Info->Details->Type->Who->Name[0]->attributes() as $key => $val) { echo "The key is $key and the value is $val. <br>"; } exit; ?> Hope this gets you going in the right direction...
  6. There are a lot of approaches you could take to validating the data. Something like this should do: <?php $entry = "20|10|5"; $errors = 0; $numbers = explode("|", $entry); foreach($numbers as $number) { $number = (int) $number; # cast the value as an integer if ($number < 1 OR $number > 36) $errors++; } if ($errors > 0 OR count($numbers) < 3) { echo 'No. Bad.'; } else echo 'Looking good'; ?>
  7. I'm not sure if I understand the problem completely but why not: <?php // A function that prints out a list of links function my_function($links) { $html = ''; // There might be cases when the array of links is empty if (count($links) > 0) { // Open the list $html .= '<ul>' // Add the list items foreach ($links as $link) { $html .= sprintf("<li>%s</li>", htmlspecialchars($link->name)); } // Close the list $html .= '</ul>'; } return $html # or echo $html; } ?> Then you can do something like: <?php $links = Link::find_all(); echo my_function($links); ?>
  8. Since each form field name is the same, you will only get the last row's data in the submitted form data (as you mentioned). You'll need to indicate that the form data should be handled as an array by adding [] to each form field name: <input type="text" name="pickuplocation[]"> You might also consider including the pickupid as the array key (assuming it's your DB table's primary key). It's not really necessary but it might help you process the data more easily: <input type="text" name="pickuplocation[<?php echo $row['pickupid']; ?>]"> PHP will then process the form values into an array that you can loop through: <?php $pickuplocations = $this->input->post('pickuplocation'); foreach ($pickuplocations as $key => $location) { echo "The pickup ID is $key and the location is $location"; } ?> Then once you have captured and organised each row's data (probably into a nested array with pickupids as the keys), you would need to run the DB update code in a loop: <?php foreach ($organised_data as $data) { $this->db->where('pickupid', $data['pickupid']); $this->db->update('pickup', $data); } ?> Hope this makes sense...
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.