Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

1 Neutral

About AdRock

  • Rank
    Prolific Member

Profile Information

  • Gender
    Not Telling
  1. AdRock

    improve my database class

    Have a look at this PDO class. https://www.culttt.com/2012/10/01/roll-your-own-pdo-php-class/ I started with MySQLi and within a few hours switched to PDO. People I work with like MySQLi and they can't see the benefit of PDO. Drives me nuts 😫
  2. I have 2 queries that I want to join together to make one row This queries returns all rows from both tables which is what i want SELECT table_A.*, table_B.* FROM table_A INNER JOIN table_B ON table_A.code = table_B.code and this is the output table_A.id | table_A.code | table_B.id | table_B.code | table_B.complete =============================================================================================== 1 | 123456 | 1 | 123456 | yes 2 | 654321 | 2 | 654321 | no and this is the second query SELECT table_C.*, table_D.* FROM table_C INNER JOIN table_D ON table_C.code = table_D.code INNER JOIN table_B ON table_D.code = table_B.code WHERE table_B.complete = 'yes' and again the output table_C.id | table_C.code | table_D.id | table_D.field2 | table_B.complete ======================================================================================== 1 | 123456 | 1 | 123456 | yes What I've been trying to for the last couple of days is join the 2 queries together to make one query that returns this table_A.id | table_A.code | table_B.id | table_B.code | table_B.complete | table_C.id | table_C.code | table_D.id | table_D.field2 | table_B.complete ==================================================================================================================================================================================== 1 | 123456 | 1 | 123456 | yes | 1 | 123456 | 1 | 123456 | yes 2 | 654321 | 2 | 654321 | no All i want it to do is get all rows from tables A and B and return all rows from C and D only if table_B.complete equals "yes". All tables are joined by the code column which all have the same value
  3. I have an array like this $rows = array( array( 'fruit.name' => 'Apple', 'fruit.colour' => 'Red', 'fruit.weight' => '0.1', 'vegetable.name' => 'Carrot', 'vegetable.colour' => 'Orange', 'vegetable.weight' => '0.05' ), array( 'fruit.name' => 'Banana', 'fruit.colour' => 'Yellow', 'fruit.weight' => '0.7', 'vegetable.name' => 'Potato', 'vegetable.colour' => 'Brown', 'vegetable.weight' => '0.6' ) ); And i want to be able to sort the array into 2 other arrays called 'fruits' and 'vegetables' based on the first part of the key name so up to the decimal point. With this array I should have 2 rows in each of the fruits and vegetable arrays. I have this code but it doesn't work and I can't see what I'm doing wrong. $fruits = array(); $vegetables = array(); foreach($rows as $row) { foreach($row as $key => $value) { if('fruit' == substr($key, 0, strpos($key, '.'))) { $fruits[$key] = $row; } else { $vegetables[$key] = $row; } } } echo "<pre>"; var_dump($fruits); echo "</pre>"; When i do a var_dump i get this array(3) { ["fruit.name"]=> array(6) { ["fruit.name"]=> string(6) "Banana" ["fruit.colour"]=> string(6) "Yellow" ["fruit.weight"]=> string(3) "0.7" ["vegetable.name"]=> string(6) "Potato" ["vegetable.colour"]=> string(5) "Brown" ["vegetable.weight"]=> string(3) "0.6" } ["fruit.colour"]=> array(6) { ["fruit.name"]=> string(6) "Banana" ["fruit.colour"]=> string(6) "Yellow" ["fruit.weight"]=> string(3) "0.7" ["vegetable.name"]=> string(6) "Potato" ["vegetable.colour"]=> string(5) "Brown" ["vegetable.weight"]=> string(3) "0.6" } ["fruit.weight"]=> array(6) { ["fruit.name"]=> string(6) "Banana" ["fruit.colour"]=> string(6) "Yellow" ["fruit.weight"]=> string(3) "0.7" ["vegetable.name"]=> string(6) "Potato" ["vegetable.colour"]=> string(5) "Brown" ["vegetable.weight"]=> string(3) "0.6" } } Any help please getting this to separate the array into 2 arrays each containing either fruits or vegetables.
  4. Yes. That was a typo and I've narrowed it down to a form class I use that generated form elements. Actually writing the html markup does work but my form class seems to drop the zero value. I might have to set the form values to yes/no and then convert that to a 0/1 before it goes into database. Anyway thanks for your help. At least it helped my identify the problem
  5. AdRock

    Help needed in database connection

    Have a look in your php.ini and see if short_open_tag is set to on. If no, you can set it to on otherwise replace <? with <?php Also it looks like it's reading that file code that connects to database as text
  6. Whenever I try and post a form with radio buttons like this <input type="radio" name"whatever" value="0"> and I do a var_dump($_POST) it always shows array(11) {["whatever"]=> string(2) "on" } but this works and will display 1 in the var_dump <input type="radio" name"whatever" value="1"> Why is this. I need the value set to 0 because that's the value going into the database. I don't really want to do a str_replace just to replace "on" with "0"
  7. AdRock

    Help needed in database connection

    Is that the error that gets output to the screen or in the error log? A bit more information would help. How are you trying to run it etc?
  8. I have been following an article on creating bullet proof sessions but I'm having problems with session variables i'm creating getting destroyed I call the session_start() like this SessionManager::sessionStart('MySession', 0, '/', 'localhost'); But when i try to add new session vars, i think the preventHijacking() function is is getting called for some reason and it wipes out the session and creates a new one. Any ideas how I can get this to work? Here is the link http://blog.teamtreehouse.com/how-to-create-bulletproof-sessions And here is the complete code class SessionManager{ static function sessionStart($name, $limit = 0, $path = '/', $domain = null, $secure = null) { // Set the cookie name session_name($name . '_Session'); // Set SSL level $https = isset($secure) ? $secure : isset($_SERVER['HTTPS']); // Set session cookie options session_set_cookie_params($limit, $path, $domain, $https, true); session_start(); // Make sure the session hasn't expired, and destroy it if it has if(self::validateSession()) { // Check to see if the session is new or a hijacking attempt if(!self::preventHijacking()) { // Reset session data and regenerate id $_SESSION = array(); $_SESSION['IPaddress'] = $_SERVER['REMOTE_ADDR']; $_SESSION['userAgent'] = $_SERVER['HTTP_USER_AGENT']; self::regenerateSession(); // Give a 5% chance of the session id changing on any request } elseif(rand(1, 100) <= 5) { self::regenerateSession(); } } else { $_SESSION = array(); session_destroy(); session_start(); } } static protected function preventHijacking() { if(!isset($_SESSION['IPaddress']) || !isset($_SESSION['userAgent'])) return false; if ($_SESSION['IPaddress'] != $_SERVER['REMOTE_ADDR']) return false; if( $_SESSION['userAgent'] != $_SERVER['HTTP_USER_AGENT']) return false; return true; } static function regenerateSession() { // If this session is obsolete it means there already is a new id if(isset($_SESSION['OBSOLETE'])) return; // Set current session to expire in 10 seconds $_SESSION['OBSOLETE'] = true; $_SESSION['EXPIRES'] = time() + 10; // Create new session without destroying the old one session_regenerate_id(false); // Grab current session ID and close both sessions to allow other scripts to use them $newSession = session_id(); session_write_close(); // Set session ID to the new one, and start it back up again session_id($newSession); session_start(); // Now we unset the obsolete and expiration values for the session we want to keep unset($_SESSION['OBSOLETE']); unset($_SESSION['EXPIRES']); } static protected function validateSession() { if( isset($_SESSION['OBSOLETE']) && !isset($_SESSION['EXPIRES']) ) return false; if(isset($_SESSION['EXPIRES']) && $_SESSION['EXPIRES'] < time()) return false; return true; } }
  9. Is there a nicer, cleaner more efficient and less code way of achieving this? I have an array of rows from the database and 2 of the columns could have either of 2 values which give me 4 possibilities. I want to loop through each of them and assign the row to one of 4 arrays so they are all separated out. This is what i have so far. Haven't tested it yet but the theory makes sense. if(!empty($rows)) { foreach($rows as $key => $row) { switch($row['type']) { case 'post': switch($row['archived']) { case 0: $array1[$key] = $row; break; case 1: $array2[$key] = $row; break; default: } break; case 'event': switch($row['archived']) { case 0: $array3[$key] = $row; break; case 1: $array4[$key] = $row; break; default: } break; default: } } }

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.