Jump to content

All Activity

This stream auto-updates     

  1. Today
  2. Multilingual Support

    W3Schools should feature this support in order to reach more people. So what do you think about this?
  3. Hello folks, I want to hide a text inside a image(users given image using file upload). Thanks in advance
  4. The CodexWorld DB Class

    Please find below a copy of the entire class and a list of the occurrences of the property call $this->db. In the end can you explain the overall use of this statement? Why, for example, would $this->db not be listed as $db in the parameter list at the outset? Is this because it is a working variable within the class that takes only dynamically assigned values? ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:22: $this->db = $conn; ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:56: $result = $this->db->query($sql); ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:102: $insert = $this->db->query($query); ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:103: return $insert?$this->db->insert_id:false; ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:138: $update = $this->db->query($query); ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:139: return $update?$this->db->affected_rows:false; ~/Sites/reflexive/javascript_practice/ajax/crud/db.php:162: $delete = $this->db->query($query); <?php /* * DB Class * This class is used for database related (connect, insert, update, and delete) operations * @author CodexWorld.com * @url http://www.codexworld.com * @license http://www.codexworld.com/license */ class DB{ private $dbHost = "..."; private $dbUsername = "..."; private $dbPassword = "..."; private $dbName = "..."; public function __construct(){ if($this->dbName){ // Connect to the database $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName); if($conn->connect_error){ die("Failed to connect with MySQL: " . $conn->connect_error); }else{ $this->db = $conn; } } } /* * Returns rows from the database based on the conditions * @param string name of the table * @param array select, where, order_by, limit and return_type conditions */ public function getRows($table,$conditions = array()){ $sql = 'SELECT '; $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*'; $sql .= ' FROM '.$table; if(array_key_exists("where",$conditions)){ $sql .= ' WHERE '; $i = 0; foreach($conditions['where'] as $key => $value){ $pre = ($i > 0)?' AND ':''; $sql .= $pre.$key." = '".$value."'"; $i++; } } if(array_key_exists("order_by",$conditions)){ $sql .= ' ORDER BY '.$conditions['order_by']; } if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ $sql .= ' LIMIT '.$conditions['limit']; } $result = $this->db->query($sql); if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){ switch($conditions['return_type']){ case 'count': $data = $result->num_rows; break; case 'single': $data = $result->fetch_assoc(); break; default: $data = ''; } }else{ if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ $data[] = $row; } } } return !empty($data)?$data:false; } /* * Insert data into the database * @param string name of the table * @param array the data for inserting into the table */ public function insert($table,$data){ if(!empty($data) && is_array($data)){ $columns = ''; $values = ''; $i = 0; if(!array_key_exists('created',$data)){ $data['created'] = date("Y-m-d H:i:s"); } if(!array_key_exists('modified',$data)){ $data['modified'] = date("Y-m-d H:i:s"); } foreach($data as $key=>$val){ $pre = ($i > 0)?', ':''; $columns .= $pre.$key; $values .= $pre."'".$val."'"; $i++; } $query = "INSERT INTO ".$table." (".$columns.") VALUES (".$values.")"; $insert = $this->db->query($query); return $insert?$this->db->insert_id:false; }else{ return false; } } /* * Update data into the database * @param string name of the table * @param array the data for updating into the table * @param array where condition on updating data */ public function update($table,$data,$conditions){ if(!empty($data) && is_array($data)){ $colvalSet = ''; $whereSql = ''; $i = 0; if(!array_key_exists('modified',$data)){ $data['modified'] = date("Y-m-d H:i:s"); } foreach($data as $key=>$val){ $pre = ($i > 0)?', ':''; $colvalSet .= $pre.$key."='".$val."'"; $i++; } if(!empty($conditions)&& is_array($conditions)){ $whereSql .= ' WHERE '; $i = 0; foreach($conditions as $key => $value){ $pre = ($i > 0)?' AND ':''; $whereSql .= $pre.$key." = '".$value."'"; $i++; } } $query = "UPDATE ".$table." SET ".$colvalSet.$whereSql; $update = $this->db->query($query); return $update?$this->db->affected_rows:false; }else{ return false; } } /* * Delete data from the database * @param string name of the table * @param array where condition on deleting data */ public function delete($table,$conditions){ $whereSql = ''; if(!empty($conditions)&& is_array($conditions)){ $whereSql .= ' WHERE '; $i = 0; foreach($conditions as $key => $value){ $pre = ($i > 0)?' AND ':''; $whereSql .= $pre.$key." = '".$value."'"; $i++; } } $query = "DELETE FROM ".$table.$whereSql; $delete = $this->db->query($query); return $delete?true:false; } }
  5. The CodexWorld DB Class

    I am now able to connect to the database via the DB class, and the error message for the statement if(!$this->db){ Although I agree that the property is undefined, I do not understand the purpose of the statement in the first place. Also, what do you mean by the term prepared statements?
  6. preg_replace explanation

    Okay, I'll need a much greater breakdown that that... First, how to understand the value for $pattern. Then, where is the second matched pattern? (it looks like we are missing one) Then, why are there no spaces in the output string? It just looks way too complicated to replace only the day, as it looks like it is matching the entire string and carrying forward some of the original string instead of just what needs to be replaced.
  7. Yesterday
  8. The CodexWorld DB Class

    The connection is failing because one or more of the host name, username, password, or database name are incorrect. In that case though, it should print a connection error message. Does the error message you see refer to the line that calls the query method on $this->db? That's how you refer to an object member. http://php.net/manual/en/language.oop5.properties.php Well, I can only comment on what I'm seeing. If the code you showed above is all that's there, then it shouldn't ever show that error. The db object gets created in the constructor and then used in the getRows method, there's nothing else that would cause a problem. So, yes, the cause of the error message is likely somewhere that I haven't seen. Protection against SQL injection attacks. Based on what's there, and again, apparently it's not everything, but based on what's there it's up to you to sanitize all of the values. If you used prepared statements then you don't need to sanitize anything to protect the database. That's just class member visibility. Public, protected, or private members aren't going to affect database security.
  9. The CodexWorld DB Class

    Yes, I agree. It appears that database connection is failing, but why? My test for instantiation proves positive. Can you explain the likely meaning of the following statement? $this->db It does not appear to refer to anything. Is it possible for it to refer to something that could appear later in the class. I have only exhibited a portion of the entire class. Also, what kinds of protection were you thinking of? All of the parameters are private. Please respond before the weekend begins, and have a good weekend yourself. You have been very helpful.
  10. SQL Join - Odd results

    Well... leave it to me to "break it"... lol Thanks for your help!
  11. SQL Join - Odd results

    That message wasn't directed at you, there was another user that got banned and his posts removed, including the one I replied to You can probably leave out one of those conditions, since you're requiring that the 2 SKU columns are the same, you only need to check the value of one of them instead of both of them. That output seems strange though, I've never seen it return a column without a name.
  12. The CodexWorld DB Class

    That class doesn't seem all that useful, it doesn't provide any protection and really only gives a way to send an array of conditions for a select query. I would suggest using PDO instead, or looking into prepared statements with mysqli and using those. The first error message sounds like the database connection failed.
  13. I don't see Javascript errors, so it looks like that's working.
  14. The CodexWorld DB Class

    <?php /* * DB Class * This class is used for database related (connect, insert, update, and delete) operations * @author CodexWorld.com * @url http://www.codexworld.com * @license http://www.codexworld.com/license */ class DB{ private $dbHost = "..."; private $dbUsername = "..."; private $dbPassword = "..."; private $dbName = "..."; public function __construct(){ if(!$this->db){ // Connect to the database $conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName); if($conn->connect_error){ die("Failed to connect with MySQL: " . $conn->connect_error); }else{ $this->db = $conn; } } } /* * Returns rows from the database based on the conditions * @param string name of the table * @param array select, where, order_by, limit and return_type conditions */ public function getRows($table,$conditions = array()){ $sql = 'SELECT '; $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*'; $sql .= ' FROM '.$table; if(array_key_exists("where",$conditions)){ $sql .= ' WHERE '; $i = 0; foreach($conditions['where'] as $key => $value){ $pre = ($i > 0)?' AND ':''; $sql .= $pre.$key." = '".$value."'"; $i++; } } if(array_key_exists("order_by",$conditions)){ $sql .= ' ORDER BY '.$conditions['order_by']; } if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ $sql .= ' LIMIT '.$conditions['limit']; } $result = $this->db->query($sql); if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){ switch($conditions['return_type']){ case 'count': $data = $result->num_rows; break; case 'single': $data = $result->fetch_assoc(); break; default: $data = ''; } }else{ if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ $data[] = $row; } } } return !empty($data)?$data:false; } } $test = new DB(); if ($test instanceof DB) { echo 'Instantiated'; } else { echo 'Uninstantiated. Include failed.'; } $conditions = []; $tbl_name = 'rss2_podcast_item'; $test->getRows($tbl_name, $conditions); function getRows($mysqli_obj, $table,$conditions = array()){ $sql = 'SELECT '; $sql .= array_key_exists("select",$conditions)?$conditions['select']:'*'; $sql .= ' FROM '.$table; if(array_key_exists("where",$conditions)){ $sql .= ' WHERE '; $i = 0; foreach($conditions['where'] as $key => $value){ $pre = ($i > 0)?' AND ':''; $sql .= $pre.$key." = '".$value."'"; $i++; } } if(array_key_exists("order_by",$conditions)){ $sql .= ' ORDER BY '.$conditions['order_by']; } if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit']; }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){ $sql .= ' LIMIT '.$conditions['limit']; } $result = $mysqli_obj->query($sql); if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){ switch($conditions['return_type']){ case 'count': $data = $result->num_rows; break; case 'single': $data = $result->fetch_assoc(); break; default: $data = ''; } }else{ if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ $data[] = $row; } } } return !empty($data)?$data:false; } ?> <?php $tbl_name = 'rss2_podcast_item'; $conditions = []; print_r(getRows($mysqli_obj,$tbl_name,$conditions)); ?> Please find above two sets of code and a quoted error message from the first set. The first set of code is a portion of a PHP class called DB. The second set of code is extracted from the first and modified in order to test the integrity of the function. At the bottom of each block of code is included the code necessary to call the getRows() method/function. Whereas the method of the class fails, the extracted function does not. In both cases the same database and table are accessed with success. I am suspicious of the way in which the class is constructed. I have tried several modifications, but none of them appear to work. Roddy
  15. SQL Join - Odd results

    When I included the <> ' ' clause, I got two results. Both of which I know are valid. However, when I added the clause, I got NULL in the last two columns without column names. I find that strange. I would think that I would at least get a column name. Did I do something wrong? Here is my updated statement: SELECT TABLE1.`handle`, TABLE1.`Variant SKU`, TABLE1.`Variant Barcode`, TABLE2.`Variant SKU`, `TABLE2`.`Variant Barcode` FROM TABLE1 inner JOIN TABLE2 on TABLE1.`Variant SKU` = TABLE2.`Variant SKU` and TABLE1.`Variant SKU` <> ' ' and TABLE2.`Variant SKU` <> ' ' and TABLE1.`Variant Barcode` <> TABLE2.`Variant Barcode`
  16. jQuery each function

    You would have to use document.createElement('input'); then assign necessary attributes to it, then append it, OR create input as you normally would in html as a string, and append that.
  17. SQL Join - Odd results

    What the ???? I have no idea what you are talking about. I am here to solve a problem. Why in the world would you even bring up SPAM links in my signature. Obviously I am not a pro like you, and that is WHY I am here. It has been years since I done anything with SQL. And when I did, about 10 years ago, it was on the IBM AS400 platform. I wish I would have kept my notes. If I would have, I wouldn't be here. I appreciate any and all help that I can get. Maybe one day it will all come back to me. Sorry to be a burden by asking questions.
  18. jQuery each function

    Thanks for your response, It's works perfectly, return (value !== ""); //it was for the end of the loop, but I think it was necessary in fact Thanks, And, is there a way to put an input for each label?
  19. jQuery each function

    You are not getting the value, but text content of option list? Every time the loop completes a cycle it overrides the previous html, you need to add to previous, then at the end add to label html var join = ""; $.each(tous, function(index, value) { join += value + "<br>"; //return (value !== ""); ///// why? }); $("#label").html(join); I think this is what you are asking for? As I said, you are using innerHTML not value, so you need to adjust this to your requirements. instead of var l = selectliste.options[selectliste.selectedIndex].innerHTML; you could use var l = $(this).val(); // for value or var l = $(this).text(); // for actual text description
  20. jQuery each function

    Hello, $(function () { $('#selectliste').change(function () { var l = selectliste.options[selectliste.selectedIndex].innerHTML; var tous = l.split(' '); $.each(tous, function(index, value) { $("#label").html(value) + "<br>"; return ( value !== "" ); }); }) }); here, I want to get the value of my select box which are in string type, after I explode the string in array, the set the value of array in label, But, all worked wonderfully until $("#label").html(value) + "<br>"; which only retrieves the last list of my array. How can I make to recover all the value of my array, thank you!
  21. http://www.grammarcaptive.com/podcast_dev_copy.php
  22. Sorry. I mistyped. http://www.grammarcaptive.com/podcast_dev_copy.php
  23. You gave me the clue I needed. This appears to do the trick. <?php if(!empty($_SESSION['podcast_no_item'])) { echo "<p id='today'>Podcast No. " . $_SESSION['podcast_no_item'] . "</p>"; $podcastNo = $_SESSION['podcast_no_item']; echo "<script type='text/javascript'>podcastNo = " . $podcastNo . ";</script>"; } ?> <script> $( document ).ready(function() { var podcastInsert = $('#podcast_insert').html(); if (typeof podcastNo != "undefined" && podcastNo !== null) { $('#main').html(podcastInsert); } }); </script> Please try the following two links in the order given and tell me if the desired result has been achieved: http://www.grammarcaptive.com/podcasts_dev_copy.php http://www.grammarcaptive.com/sender_proxy.php Roddy
  24. That's because you set it to false right before you alert it. Go back and look at the code I posted, notice how the variable definitions are outside the load handler. They first get defined with those default values, maybe at some point later down the page PHP will redefine them, and then once the page finishes loading the rest of the Javascript runs, using either the default values or the values that were set by PHP.
  25. That is true as long as you're using a page load handler to kick off the Javascript. If you're not doing that, then placement does matter and the values might not be in place when it gets executed. You need to only execute the code to run your page after the page finishes loading.
  26. This is the code that I finally settled for. Unfortunately, the test alert shows the value of podcastNo to be false -- with or without the $(document).ready() function. <?php if(!empty($_SESSION['podcast_no_item'])) { echo "<p id='today'>Podcast No. " . $_SESSION['podcast_no_item'] . "</p>"; $podcastNo = $_SESSION['podcast_no_item']; echo "<script type='text/javascript'>podcastNo = " . $podcastNo . ";</script>"; } ?> <script> $( document ).ready(function() { var podcastNo = false; alert(podcastNo); var podcastInsert = $('#podcast_insert').html(); if (podcastNo !== false) { $('#main').html(podcastInsert); } }); </script> You can see the above code in podcasts_dev_copy.php. It appears at line 188 in the <nav> element. Roddy
  27. i understand the concept: Write the Javascript with everything well-defined and substitute the existing values with true values via Javascript embedded in PHP. As the PHP script is executed even before the HTML string reaches the Browser for processing, the true values are already in place before the remaining Javascript is loaded. Thus, with the exception of placing the script that creates the substitution in the replacement <div> the placement of the script should make no difference. Still i tried placing the code in different places, but it appears to make no difference. Notice that when the data reaches the podcasts.dev.copy.php page from the simulated third-party link clicking on the words Podcast No. 60 realizes the desired effect. Unfortunately, this effect should already be in place when the page loads. Triggering the effect via the phrase Podcast No. 60 acts as a pseudo page refresh. Roddy
  1. Load more activity
×