PHP - Efficiency: Array_intersect Vs Many-to-many
Hello
A question popped up in my head, while I was designing my database (this is both php and SQL thought). The scenario is that an item can have many categories. Yet the categories are finite (<100 let's say). I have no problem making a many to many link table. Yet I was wondering if the finite categories would necessitate this method. My thought is - make the categories table a default array and match it against a queried item (containing an array of category IDs) with an array_intersect. Is this efficient, silly, or a variable scenario? Thanks for any insight. Similar TutorialsHello, I have three arrays of integers $arrayA, $arrayB, and $arrayC. $ArrayA is a "master array" meaning the array I need to match the other two to. $arrayB and $arrayC are generated by my functions in the application and meet specific criteria. As such, arrays B and C will never have any intersecting values, but both should have matches in $arrayA, and I need to generate an array which contains only the values of arrayA which match either B or C. After finding some documentation here (http://www.metrocomp.mocsi.eu/w3/php/func_array_intersect.html) I tried using array_intersect : array_intersect ($arrayA, $arrayB, $arrayC) However this does not work as described and returns no results as there are no values which are contained in all three arrays. I wrote the below as a work around, and it works just fine but I would think there is a better way? $temp1 = array_intersect($arrA, $arrB); $temp2 = array_intersect($arrA, $arrC); $temp3 = array_merge($temp1, $temp2); $final_array = array_unique($temp3); Thanks for looking! Carmen Just looking for an opinion on which one of these two pieces of code would process faster: <tr align="center" class="border"> <td class="border">'; if($copper=="yes"){ $content.= '<span class="strike">Copper</span>'; } else{ $content.= 'Copper'; } $content.=' </td> <td class="border"> '; if($copper=="yes"){ $content.= '<span class="strike">$10 - $35</span>'; } else{ $content.= ' $10 - $35'; } $content.=' </td> <td class="border">'; if($copper=="yes"){ $content.= '<span class="strike">800</span>'; } else{ $content.= ' 800'; } $content.=' </td> <td class="border">'; if($copper=="yes"){ $content.='Levels that are crossed out are full.'; } else{ $content.='Name in end Credits + Nickel Level'; } $content.=' </td> </tr> vs. if($copper=="yes"){ <tr align="center" class="border"> <td class="border"> <span class="strike">Copper</span> </td> <td class="border"> <span class="strike">$10 - $35</span> </td> <td class="border"> <span class="strike">800</span> </td> <td class="border"> Levels that are crossed out are full. </td> </tr> } else{ <tr align="center" class="border"> <td class="border"> Copper </td> <td class="border"> $10 - $35 </td> <td class="border"> 800 </td> <td class="border"> Name in end Credits + Nickel Level </td> </tr> } Hello,
I hope it's ok to ask this question here. I have a registration script, but I'm not sure how to handle it efficiently and I have some questions about it. This is used in the page 'signup.php'. The class is called 'User'. I haven't noticed any errors or bugs. It would be very useful for me to be aware of my mistakes. public function regUser($uname,$upass,$upassverify) { $new_password = password_hash($upass, PASSWORD_DEFAULT); if(!password_verify($upassverify, $new_password)) { // passwords are not the same (I thought it would be better to do this after hashing, but maybe it doesn't matter or it's worse. I'm not sure about it) $info = 'pass_err'; } $stmt1 = $this->db->prepare("SELECT * FROM users WHERE username=:uname"); $stmt1->execute(array(':uname'=>$uname)); if($stmt1->rowCount() > 0) { // this username has already been used $info = 'user_err'; } if (!$info) { $stmt2 = $this->db->prepare("INSERT INTO users(username,password) VALUES(:uname, :upass)"); $stmt2->bindparam(":uname", $uname); $stmt2->bindparam(":upass", $new_password); $stmt2->execute(); // succesfully made an account $info = "success"; } header("Location:/signup.php?status=".$info); exit(); }Am I using the prepared statements as how I should be using them? Is this a safe way of handling my data or do you see vulnerabilities? I'm using PRG to prevent resubmission but I want to show a 'everything is fine' or 'oh no, something went wrong' to the one who is signinup. If I now go to signup.php?status=success, i see 'eveything is fine', without actually signing up, is there a better way to do this or can I somehow prevent everyone being able to see this? As you might have noticed in my last post, my English is not very good, sorry about that. Thanks, Fabian Edited September 17, 2019 by FabelIs it more efficient to concat strings to variables or include variables within a double quote enclosed string? I've created a couple of classes to help take tons of coding off of the front end, and I've been pondering the idea of creating a container class to hold specific objects of some classes. My container class methods will do a lot of iterating through a lot of varient object names, and I'm questioning efficiency in regards to resolving the object variable's names... Which is more efficient? ${"type_$i"}->setAttribute('readonly', 'readonly'); ${'type_'.$i}->setAttribute('readonly', 'readonly'); |