PHP - Proper Way To Clear Out/flush An Array?
What is the PROPER way to clear out an array?
I have an array getting created as part of a loop process, and for each loop, I want the array to be emptied and start from scratch? I googled it and see some ways but the people offering the solutions don't seem too sure of themselves, so I just want to know if there is one, widely accepted and proper/efficient way to do this? Similar Tutorials$sql = "SELECT option_id, id FROM bets WHERE win_status = '1' AND updated_at BETWEEN '$today 13:00:00.000000' AND '$tmrw 08:00:00.000000'"; $result = mysqli_query($connt, $sql); foreach($result as $value) { $value[] = arary_multisort(); $oid = $value['option_id']; $gid = $value['id']; var_dump($value); //$gim = var_export($value); } foreach($result as $key => $value) { $info = array($value); } mysqli_close($connt); $fl = array($oid, $gid); print_r($fl);
array ( Either I can return 1 row and have the data sorted nicely or I can return an array but I can only seem to access the last array. Been stuck on this bit for atleast 2 hrs sadly. How can I rename the array? Some days I might have 100 results. Today we have two winners xP I really need to ctrl+z sublime cuz I tried what feels like everything. Ancy to move to the next part of this code MATH if you can kindly assist. Yall have been extremely helpful in my last two posts 2/2 going on 3. Is there a place I can properly contribute lite silver :3 Hey guys! I found a flush function and it seems to be working well, but sometimes it will cut off characters. It's very strange. Here's my flush function: function out($str) { echo $str."<br />\n"; vbflush(); } function vbflush() { if (ob_get_length()){ @ob_flush(); @flush(); @ob_end_flush(); } @ob_start(); }And I'm calling it like this:out('Waiting 30 seconds until execution');And this is my output: Code: [Select] Waiting 30 seconds until executioAnyone know why? I have looked at ob_start, ob_get_contents, etc.. but I can't get it to work. My code looks like this: Code: [Select] ob_start(); echo "starting"; ob_flush(); //do some stuff echo "something else" ob_flush(); //do some stuff echo "something else" ob_flush(); The echoes are only shown at the end of the script execution. Say I have two areas of a website. 1 - root directory (index, sign up, sign in) 2 - folders (this would contain folders such as snippets, members, assets) Due to the nature of the folders, the links cannot be the same as they are on the pages in the root directory. Index page for example. So if I'm on members/dashboard.php, any a links linking to index.php, will have to have "../" in front of them. To solve the issue, my current set up is like this. But I understand it's not the most efficient way to do this. I was wondering if you can share your expertise for a better method. $currentPage = basename($_SERVER['PHP_SELF'], ".php"); <?php if($currentPage == 'index' || $currentPage == 'members') { ?> <!DOCTYPE HTML> <head> <meta charset="UTF-8"> <title></title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <link rel="shortcut icon" href="images/favicon.ico.png"> <link href="css/screen.css" media="screen" rel="stylesheet" /> </head> <body> <a href="index"> <img src="images/logo.PNG" alt="logo" /> </a> </body> <?php } else { ?> <!DOCTYPE HTML> <head> <meta charset="UTF-8"> <title></title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <link rel="shortcut icon" href="../images/favicon.ico.png"> <link href="../css/screen.css" media="screen" rel="stylesheet" /> </head> <body> <a href="../index"> <img src="../images/logo.PNG" alt="logo" /> </a> </body> <?php } else { ?>
Should this be the proper way of adding the getUsername() text in the query below: Code: [Select] mysql_query("UPDATE posts SET content = '$content', lastedit = ". getUsername() .":NOW() WHERE id = '$pid'") or die(mysql_error()); Im having trouble with this wondering if someone could help me out please if (isset($_POST['escape'])) { || if (isset($_POST['suicide'])) { its giving me a error on the || im writing it so that one or the other has to happend. is this the correct way to use this? Just curious, what is proper programming in cases where you're just doing a simple IF statement where there really is no need for an ELSE. Should you still write out the ELSE? For example, which is proper... Code: [Select] if (x=1) { //code here to do something } or Code: [Select] if (x=1) { //code here to do something } else { } There would be nothing in the ELSE statement, so is it OK to just leave it out completely (i.e. version #1), or is it proper to still have it listed (i.e. version #2)? Hi guys, I'm starting to get back into coding for a hobby and wondering is there a better way of doing these php and mysql calls? they all work but not sure if it was the efficent way or is there others that everyone else uses?
//connection for user details lookup
while($row2 = $result2->fetch_assoc()) {
//connection for role access against business lookup
while($row3 = $result3->fetch_assoc()) { Hi. Currently I have code the uses both TRUE. IF statements and FALSE statements to move on. For example. Is it better to use: Code: [Select] Where B=A C=A IF A=B { IF C=B{ ECHO 'HELLO WORLD'; }ELSE ECHO 'C DOES NOT EQUAL B'; }ELSE ECHO 'A DOES NOT EQUAL B'; ' OR is it better to do it like this: Code: [Select] Where B=A C=A IF A<>B { ECHO 'C DOES NOT EQUAL B'; ELSE IF C<>B{ ECHO 'C DOES NOT EQUAL B'; ELSE ECHO 'HELLO WORLD'; '[/code That's right I'm pretty green when it comes to PHP. I googled it, but it looks like there are several ways to achieve this, wondering which is best.
elseif ($subcategory_id == 400)I want the code to include a number of subcategory_ids, 400, 450, 500, 760, etc. There isn't a range, so what would be the proper way to include multiple category_ids. Thank you Code: [Select] if (isset($select_category) || isset($most_liked)) { // ALL but NO OTHER if (((($select_category == 'All') || (!isset($select_category)))) && (!isset($most_liked))) { $query = "SELECT * FROM con, user WHERE con.user_id = user.user_id ORDER BY contribution_date DESC"; fetch_all ($dbc, $query); // CATEGORY but NOT most_liked } elseif (isset($select_category) && !isset($most_liked)) { $query = "SELECT * FROM con, user WHERE con.user_id = user.user_id AND category = '$select_category' ORDER BY contribution_date DESC"; fetch_all ($dbc, $query); // CATEGORY and MOST_LIKED } elseif (($select_category != 'All') && (isset($select_category) && isset($most_liked))) { $query = "SELECT * FROM con, user WHERE con.user_id = user.user_id AND category = '$select_category' ORDER BY likes DESC"; fetch_all ($dbc, $query); // ALL or NO CATEGORY but MOST LIKED } elseif ((($select_category == 'All') || (!isset($select_category))) && (isset($most_liked))) { $query = "SELECT * FROM con, user WHERE con.user_id = user.user_id ORDER BY likes DESC"; fetch_all ($dbc, $query); } } What I am basically trying to accomplish is, choose a category through a drop down list, select that category and then go through all the rows in the database and print the content out. As of now it is working for me, I am also using a pagination script to print out everything in a table. The problem I am having is that fetch_all function is also printing the query out together with the content. Any ideas why it does that? Did I use this function the right way? If you need more information let me know. Hello everyone, I've recently asked a question about forms and Requinix mentioned PRG method of processing forms. The PRG idea solves my refresh and back button document expired problems but i notice something new: when i refresh a page or use the browser back button - then return to the PRG process - repeat a refresh or back button action - i notice that i can traverse the cache history as many times as my prg approach redirects me. I feel like i am not implementing this PRG method correctly, or is this correct? Here is the form process if it helps solve the problem: i have a login form which contains a CSRF token. when i submit the form i specify an action as /prg/ which submits the data to index.php in the prg folder. After processing the data, prg index.php redirects to the root (because you are logged in). One problem is that i have a logo on the login page that allows you to return to the root (localhost index page). When the form is not submitted or it contaiuns incorrect login data or the logo is clicked to return to homepage, then the history seems to repeat itself. I've read that people recommend to use a header 303 See Other but the result is the same. Basiclly, if i am implementing a prg correctly, then the question becomes how can i instruct a browser to ignore the redirect as if the cache contains only the index page? i cannot find prg examples that involve a csrf token and other links on the protected page (prg/index.php protected because you need a form submission with a csrf token and it only processes data then redirects.) I don't see this happening in professional sites like Google or Microsoft etc. what am i doing wrong? Hi Guys,
Total PHP Noob here. I’ve run into a brick-wall and would appreciate some words of wisdom. I can do this with a browser: http://86.60.161.24 // will echo hello World I can’t do this with my server: <?php $url = 'http://86.60.161.24'; $contents = file_get_contents($url); echo ($contents); ?> It looks like the server-side HTTP -request never “touches” my Arduino-chip. No network activity detected. Obviously there is going to be a lot more data transfer happening but I need to solve this issue first. Is there anything I can do to make this work? Any and all input greatly appreciated. Cheers!!
This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=345920.0 Hello everyone. First post here. very new to php and html. Im working on my uncles website which is a fabric store. I created a mysql database and a search to use. Im trying to make this a fulltext search because right now you have to match the pattern name exactly to get any results! And if somebody searches for the pattern name and the color they will get no results! In fact, if there is more than one search term, there is no results. -This is the first part where it counts so the pagination can be built- $sql = "SELECT COUNT(*) FROM rapatterns WHERE Pattern= '$keyword' OR Color= '$keyword' OR Fabric_Use= '$keyword'"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); -here is the query to get results- $sql = "SELECT * FROM rapatterns WHERE Pattern= '$keyword' OR Color= '$keyword' OR Fabric_Use= '$keyword' LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); Ive looked into full text searching, and ive created a fulltext index on the three columns being searched here. The problem im having is when I try to change the queries to fulltext searches I get syntax errors. -this is what i was trying to do- -this is the first one that counts for pagination- $sql = "SELECT COUNT(*) FROM rapatterns WHERE MATCH(Pattern,Color,Fabric_Use) AGAINST ('$keyword') LIMIT $offset, $rowsperpage"; -here is the second one to get results- $sql = "SELECT * FROM rapatterns WHERE MATCH(Pattern,Color,Fabric_Use) AGAINST ('$keyword') LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); Im wondering if I can get a little help with the syntax or if what im trying to do wont work at all. Go easy on me. I am extremely new to any of this. Hi, I'm pretty new to php and need a bit of basic help. I have a header and footer include file in a "views" folder. The header itself has a function that's included from another directory. Now when I try to include the header in by index.php in the root it throws and error saying it can't find my function include. Obviously the include directory is relative to the file it's included in. So, what's the trick to fixing this problem? And what's the lesson for future projects? Thanx in advance. Hi All, Can any body let me know what is proper way to write below code? Quote while($row = mysql_fetch_array($result)) { for ($colq=0; $colq<=$col_no;$colq++) { while($col = mysql_fetch_array($result_display_sequences)) { for ($b=0; $b<=$seq_num;$b++) { echo $col[$b] . " "; while($asso = mysql_fetch_array($result_column_associated)) { for ($c=0; $c<=$asso_num;$c++) { echo $asso[$c] . " "; if ($col[$b] == $asso[$c]) { echo "<td>" . $row[$colq] . "</td>"; } } } } } } } I want to match two values before display the result but somehow at first level its work perfect but then its didnt increase value $b as it should. So i lil but confuse now what am i doing wrong with it. Sorry i am new and trying something hard assignment while test my skill. i do get result as below 2 2 3 4 5 6 7 8 9 10 11 3 4 5 6 7 8 9 10 11 Thanks in Advance. I am using a MVC framework and in my controller I have defined a class variable for configurations. In my action I have a call to the configuration class to set the class variable to the current configurations. Code: [Select] public $configArray = array(); public function actionBuild($id) { $this->configArray=Config::model()->getConfigArray($id); $this->buildStep1(); ... } When I echo the configuration in the method it is 10 but when I echo in buildStep1 it is 11. What is the proper way for configArray to be global and updated when I call getConfigArray for use in functions in the class? I built a website that has multiple users accessing it at once, there is a problem with the sessions. People are getting directed to a logged out page. I know why this is because I don't thoroughly understand how to use sessions.
I have picked up about storing in places other than /tmp , I'm concerned about storage and not carrying it from one page to the next
So when a person logs in, then they hit back on their browser or exit it (for a phone) and go back to it, they should still be logged in. That doesn't happen, they are usually logged out.
Also there is a weird anomaly. When I use the website on my device (phone) I don't have a problem of session value getting lost and the website exists on a server / computer that is not this phone, so why does my friend have problems with sessions but not me?
This is my general session code, it is garbage
<?php ob_start(); session_start(); if( !isset($_SESSION['last_access']) || (time() - $_SESSION['last_access']) > 60 ) $_SESSION['last_access'] = time(); session_save_path('/home/admin2/public_html/sesh'); ini_set('session.gc_probability', 1); $userrname = $_SESSION["user"]; $_SESSOIN["user"]=$userrname; if (empty($userrname)){ header("Location: "); } ?>I'd appreciate any help. Thank you. I am still trying to fully comprehend table normalization and I'm not asking anyone to explain it bc it's not a simple thing to explain.
But I'm working on a football website that tracks players stats each week (catches, yards and touchdowns) and it seems like setting it up this way would make it the easiest (maybe laziest?) to retrieve data to display wherever I want...
tablecalled players with these fields:
playerid
playerfirstname
playerlastname
week1catches
week1yards
week1touchdowns
week2catches
week2yards
week2touchdowns
...
and so on 17 times till...
...
week17catches
week17yards
week17touchdowns
I know that is likely making most of you want to puke but if doing it this way makes it extremely easy to pull this info for display on the site, why is it so wrong? Is the problem that it's much slower than what I THINK is the proper way, ie...
table called players with these fields:
playerid
playerfirstname
playerlastname
AND
table called stats with these fields:
playerid
week
catches
yards
touchdowns
Again, not asking for an explanation of normalization, just if anyone has any quick reasons why the first way is as god awful as I fear it is, I'd appreciate the knowledge.
Thanks!
|