Jump to content

jay20aiii

New Members
  • Posts

    8
  • Joined

  • Last visited

jay20aiii's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks Maxxd. I had the same idea earlier on but was unsure about the implementation. I appreciate the example as it is a massive help. I'm sure you will know how time consuming it can be to try and find a basic and complete snippet that reflects what you are trying to do. I shall upvote your post tomorrow when my daily limit of (1 ?!) has expired. Jay
  2. Hi, I have had to work on other things in the mean time, but have managed to revisit this project again today. Good advice thanks. This function will run on a website on a shared host. As a compromise I think each page load should use a single database connection so I have redesigned the class to do this. I have done what you suggested. I should have updated the typo, as I had been trying various things with the function prior to posting, but the array indexes were previously correct. This was invaluable in helping me to understand what I needed to do to get each query back into an array of Class elements so thanks for taking the time to explain this. I have pasted the class below that is now working for me. If there are any other suggestions that could help improve on this please do let me know. <?php class connection { public $pdoConnection; public $mysqliConnection; public function __construct() { require_once (dirname(__FILE__) . "/../types/classes.php"); $this->open(); } public function open() { if (class_exists('PDO')) { if (!isset($this->pdoConnection)) { include (dirname(__FILE__) . "/../config.php"); $this->pdoConnection = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name . ';charset=utf8', $db_user, $db_pass); $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } } else { if (!isset($this->mysqliConnection)) { include (dirname(__FILE__) . "/../config.php"); $this->mysqliConnection = mysqli_connect($db_host, $db_user, $db_pass, $db_name); } } } public function close() { if (class_exists('PDO')) { if (!empty($this->pdoConnection)) { $this->pdoConnection = NULL; } } else { if (!empty($this->mysqliConnection)) { mysqli_close($this->mysqliConnection); } } } function byrefValues(&$arr){ $refs = array(); foreach($arr as $key => $value) $refs[$key] = &$arr[$key]; return $refs; } public function mysqlSelectToClass($query, $className, $args = NULL) { $this->open(); if (class_exists('PDO')) { $dbQuery = $this->pdoConnection->prepare($query); if (isset($args)) { $type = array_values($args)[0]; $params = array_values($args)[1]; for ($i = 0; $i <= count($params); $i++) { $param = $params[i]; $paramType; if ($i<=strlen($type)) { switch ($type[i]) { case 'i': $paramType = PDO::PARAM_INT; break; default: $paramType = PDO::PARAM_STR; } } else { $paramType = PDO::PARAM_STR; } $dbQuery->bindParam(array_values($param)[0], array_values($param)[1], $paramType); } } $dbQuery->execute(); $result = $dbQuery->fetchAll(PDO::FETCH_CLASS, $className); } else { $dbQuery = $this->mysqliConnection->stmt_init(); if ($dbQuery->prepare($query)) { if (isset($args)) { $type = array_values($args)[0]; $params = array_values($args)[1]; call_user_func_array('mysqli_stmt_bind_param', array_merge(array($dbQuery, $type), $this->byrefValues($params))); $dbQuery->execute(); $dbResult = $dbQuery->get_result(); $result = array(); while ($item = $dbResult->fetch_object()) { array_push($result, $item); } } elseif ($dbResult = mysqli_query($this->mysqliConnection, $query)) { $result = array(); while ($item = $dbResult->fetch_object()) { array_push($result, $item); } } } } return $result; } } ?> Cheers Jay
  3. I have a function that performs a SELECT query on a MySQL database and populates the results in an array of Class. At the moment it is using PDO. Trouble is that PDO is not supported by the server the code will run on. Changing server is not an option, nor is installing PDO. I have tried splitting the function to use the PDO method if installed or MySQLi if not. I am struggling to get the MySQLi part working though. Can anyone help me with this? Here is the function I have so far which basically returns nothing from the MySQLi part: public function mysqlSelectToClass($query, $className, $args = NULL) { include (dirname(__FILE__) . "/../config.php"); if (class_exists('PDO')) { $db = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name . ';charset=utf8', $db_user, $db_pass); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbQuery = $db->prepare($query); if (isset($args)) { foreach ($args as $arg) { $dbQuery->bindParam(array_values($arg)[0], array_values($arg)[0], PDO::PARAM_STR); } } $dbQuery->execute(); return $dbQuery->fetchAll(PDO::FETCH_CLASS, $className); } else { $db = mysqli_connect($db_host, $db_user, $db_pass, $db_name); $dbQuery = $db->prepare($query); if (isset($args)) { // Type is a string of parameter types e.g. "is" $type = array_values($args)[0]; // Params is an array of parameters e.g. array(1, 'value') $params = array_values($args)[1]; call_user_func_array('mysqli_stmt_bind_param', array_merge(array($dbQuery, $type), $this->byrefValues($params))); $result = mysqli_stmt_execute($dbQuery); mysqli_close($db); } elseif ($dbResult = mysqli_query($db, $query)) { $result = mysqli_fetch_object($dbResult, $className); mysqli_close($db); } return $result; } } the byrefValues function is simply swapping a value array to a reference array and seems to be working fine. I can paste that too if required. Thanks Jay
  4. Hi what would be the best way to separate and indent html tags taken from a source I have no control over. For example some rows may have leading/trailing tabs and spaces. Multiple code tags can appear in a single line. Closing tags may be inline, on a new line or even follow empty lines. (Basically a spaghetti soup of html code). And if possible, I would like to include this html within my own (already formatted) html. So let's say the include was within a tag that was 2 tabs deep. I presume I could somehow use a function that would add two tabs to start of each row after formatting the spaghetti, in order to make it follow the flow of my own code. Is there a simple method to do this already? Would I need to code one, and what would be the best way to optimize such a method? Thanks
  5. Headers and boundary headers need to be escaped using two line feeds (to create an empty row) in emails. So try changing the last row of your $header variable to end with "\r\n\r\n". I read somewhere though that the "\r" may not be needed (I forget why) but you can try with and without. HTH
  6. Cool, working as it should be now thanks. Good point about the textContent.
  7. 1). Thanks 2). Can you tell me where I am going wrong on this loop: $element = $document->getElementById('txtTitle'); $options = $element->getElementsByTagName("option"); foreach($options as $option) { if ($option->textContent = $_POST['txtTitle']) { $option->setAttribute("selected", "selected"); } } The resulting html has an empty "selected" attribute on every Option tag.
  8. Hi i'm new to PHP and the forum. I am working on a email script. In it I am using the following to highlight any Input values the user forgot to fill in. $html = file_get_contents("page.html"); $document = new DOMDocument(); @$document->loadHTML($html); if (empty($_POST['txtEmail'])) { $document->getElementById('email')->setAttribute('style', "color: #ff0000"); } else { $document->getElementById('txtEmail')->setAttribute('value', $_POST['txtEmail']); } print_r($document->saveHTML()); I have two questions... 1). I read print_r should be used primarily for debugging. Should I save the html changes to file and then navigate to that file.. Something like: $document->saveHTMLFile('somepage.html'); header('Location: http://www.domain.com/somepage.html'); Or is there in fact a better, or more suitable, way using only PHP? 2). In my snippet I can update the color of a Label element if a value is missing, or populate the previous Post value if it was present (but a different required value was missing). But how would I go about selecting the option of a Select element? e.g. if my html was: <label id="title"><span class="fieldname">Title:</span> <select id="txtTitle" name="txtTitle"> <option value="">Select</option> <option value="DR">Dr </option> <option value="MR">Mr</option> <option value="MRS">Mrs</option> <option value="MS">Ms</option> <option value="MISS">Miss</option> <option value="OTHER">Other</option> </select> </label> and the post data of txtTitle was "Mr", how would I select Mr in the DomDocument e.g. how would I ammend this line: <option value="MR">Mr</option> to this: <option value="MR" selected="selected">Mr</option> Hope I was clear. Thanks Jay
×
×
  • 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.