Vel
Members-
Posts
130 -
Joined
-
Last visited
Everything posted by Vel
-
Yes, that's exactly what I was after. Thank you very much. Never heard of the pow function before.
-
OK, in the code above the function goes from 0 up to an unknown number (max of 11). Each loop through it tests the result row[0] for the next bit to see if it is set. So first loop through it is if(($row[0] & 1) == 1) $hours[$j]++; Second loop through it's if(($row[0] & 2) == 2) $hours[$j]++; Third... if(($row[0] & 4) == 4) $hours[$j]++; If you see my OP that number is a variable, $bwt. That is determined by a manually filled in array right now. However what I want is a mathematical function to get that number for me, so I can put in the bit, and get the decimal value for it: i.e. Bit = Decimal 1 = 1 2 = 2 3 = 4 4 = 8 5 = 16 6 = 32 7 = 64 8 = 128 and so on.
-
So in binary the first bit = 1, the second bit = 2, the third bit = 4, the fourth = 8 and so on. I have manually filled in an array for that. If I specify the bit, is there a function that will give me the decimal value of that bit. I always thought it was 2n, but that does't work.
-
I'm going through a loop and using an AND check to see if an appropriate flag has been set, and if so increase the appropriate hour by 1. Code is (without all the SQL getting the data): <?php while($row = $result->fetch_row()) { for($j = 0; $j < $bat->length; $j++) { $bwt = $bitwise_array[$j]; if(($row[0] & $bwt) == $bwt) $hours[$j]++; } } Right now bitwise array is just a manually populated array with 1,2,4,8,16 etc... However is there a mathematical function for calculating the proper bit in decimal?
-
If you include the file anything in that file will be outputted on index.php.
-
Thanks for the advice guys. I appreciate it.
-
Sorry, I mistyped it in my haste. I do use isset to check if it exists, and if it does increment it by 1, otherwise set it to 1.
-
What you need to do is add an extra variable to the form as a hidden variable, call it form_submitted and then check for that variable before including file_upload_script.php. That way you won't get the error message if you haven't submitted the forum. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Language" content="da" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> .style1 { text-align: left; } </style> </head> <body> <p>UPLOAD FILE</p> <form enctype="multipart/form-data" method="post" action="file_upload_script.php"> <div class="style1"> Choose file:<br /> <input name="file1" type="file" /><br /> <input type="submit" value="Go" /> <input type="reset" value="Reset" /> <input type="hidden" name="form_submitted" value="1" /> </div> </form> </body> <?php if(isset($_POST['form_submitted'])) include("file_upload_script.php"); ?>
-
This is more of a question of "is it OK and / or correct" and a point of interest to myself (and hopefully others) than anything else. I have a situation where in a multidimensional array I am counting the number of entries in another part of this array. I have this statement: if($array[$i]['counter']) $array[$i]['counter']++; else $array[$i]['counter'] = 1; It would make sense to me to cut the code down and remove the if statement, and just go with: $array[$i]['counter']++; Completely ignoring the step of checking to see if it's initialised, and if not initialising it. I'm guessing this isn't totally proper, but is it completely incorrect to do this or is it an acceptable way of saving a couple of lines of code?
-
Using unset is causing a premature end of script headers error.
Vel replied to Vel's topic in PHP Coding Help
Someone told me that htaccess doesn't work on Windows. From my own experience that seemed to be true, so I took it as that. PFM pointed out I was wrong, you just posted something sarky. And this is the second time you've done that when I've posted something incorrect. I'm still learning and I know I'll get things wrong, but that's no reason to be like that about it. You'll find people much more receptive to you if you just help them instead of insulting them. -
Using unset is causing a premature end of script headers error.
Vel replied to Vel's topic in PHP Coding Help
K, if you see one of my posts, please, don't bother responding. What PFM put was helpful. You're just an ass. -
Using unset is causing a premature end of script headers error.
Vel replied to Vel's topic in PHP Coding Help
.htaccess only work on Linux. If you try to run them on windows you just get a 500 internal server error, or at least that's the result I get ever single time I've ever tried. Or maybe that's just for URL rewite, which is what we use .htaccess for. Why not? All you are trying to do is get the php code on one page to run so that you can debug it. In fact, you only need that problematic portion of the code to run. A .htaccess file has no bearing on the php code after the php code has been invoked. Good point, although it wouldn't be exactly the same as running it where it is right now, it should be similar enough. -
Using unset is causing a premature end of script headers error.
Vel replied to Vel's topic in PHP Coding Help
Unfortunately our web host forces display_errors off. Not the ideal situation, I know. I've tried forcing them on using a php_ini file to no avail. We are in the process of getting a development server to solve that problem. In the mean time, I can't work on WAMP, or something like that as the website uses .htaccess files. I've solved the problem of the -1 being outputted instead of the +1. I wasn't incrementing the counter when putting the filters into $tmp, so $tmp only had one field, yet the script expected multiple. That still doesn't solve the initial problem, although I now have a work around. I'd like to know why it was throwing a Premature end of script headers error though, if anyone can explain? -
Using unset is causing a premature end of script headers error.
Vel replied to Vel's topic in PHP Coding Help
OK, a little more information that I think totally screws my approach to this. I saw that using unset($tmp[$out]['opt']) worked, so I thought I would unset that and then check if that was unset instead, so I modified _temp_first to if(isset($tmp[$i]['opt'])) return $i; else ... Same problem, 500 error, Premature end of script headers. So, I can't check for an unset variable in my header. So next I tried to set $tmp[$out] to NULL, same problem. When checking if it exactly equals null I get internal server error, same error in the logs. I then tried setting $tmp[$out]['opt'] == ""; and $tmp[$out]['num'] == -1; and then in _temp_first check for $tmp[$i]['num'] == -1. In theory this should have worked, but for some reason the first option outputted again instead of the second option (no 500 error though, making progress). I just need to debug why the -1 was outputted instead of the +1 now. -
Your posting in the wrong section. This is for people who have written code and need specific help solving a bug in that code, or have at least attempted to have a go at writing some code.
-
Hi guys, I'm adding a filter system to out e-commerce solution and all seemed to be going well, until now. I've hit a problem that I can't find any answer to, or even an explanation as to why it is happening. I am loading all my filters into an array and then transferring them into a temp array for sorting and outputting. The way I do this is grab the first one, then go through each one and check to see if any others have more have a higher number of products using the filter. If so then that becomes the one to output and I continue through until the array is exhausted, then output whatever pointer is given to me at the end. That all works fine and the most commonly used filters appear at the top of the list. But to stop them from showing more than once, I then need to remove them from the temp array once they have been echo'd out. And this is when the problem occurs. When trying to unset the filter from the array the website throws a 500 Internal server error. The error logs show a premature end of script headers error. I have no idea why this is doing it. This is my code: class Filters { public $filters = array(); /* Class Constructor */ function Filters() { $this->load_headings(); if(count($this->filters)) $this->load_options(); } /* Load Filter Headings */ function load_headings() { global $error; $query = "SELECT sfID, sfFilter FROM sectionfilters" . (isset($_GET['cat']) ? " WHERE sfSection = " . (int)$_GET['cat'] : ""); if(!$result = mysql_query($query)) { $error->trigger(MYSQL_ERROR, "Database error whilst trying to load filter headings in filters.php. Function load_headings. Query: $query", mysql_error(), TRUE); return 'MYSQL_ERROR'; } $i = 0; while($row = mysql_fetch_assoc($result)) { $this->filters[$i]['heading'] = $row; $this->filters[$i]['num_opts'] = 0; $this->filters[$i++]['options'] = array(); } return FALSE; } /* Load Filter Options */ function load_options() { global $error; $query = "SELECT pfFilter, pfFilterID FROM prodfilters WHERE pfFilterID IN"; $start = "("; foreach($this->filters as $f) { $query .= $start . $f['heading']['sfID']; $start = ", "; } $query .= ")"; if(!$result = mysql_query($query)) { $error->trigger(MYSQL_ERROR, "Database error whilst trying to load filter options in filters.php. Function load_options. Query: $query", mysql_error(), TRUE); return 'MYSQL_ERROR'; } while($row = mysql_fetch_assoc($result)) { for($i = 0; $i < count($this->filters); $i++) { if($this->filters[$i]['heading']['sfID'] == $row['pfFilterID']) { $var = $row['pfFilter']; if(isset($this->filters[$i]['options']["$var"])) { $this->filters[$i]['options']["$var"]++; } else { $this->filters[$i]['options']["$var"] = 1; $this->filters[$i]['num_opts']++; } } } } } /* Output Filters */ function output() { global $showfilternum; // First we check to see if any previous filters are stored in session. If there are we check if they are for this category, and if not then clear them. If they are we will use them. if(isset($_SESSION['filters'])) { if($_SESSION['filters']['cat'] != @$_GET['cat']) unset($_SESSION['filters']); } $k = 0; foreach($this->filters as $f) { if($f['num_opts']) { echo '<div class="filter-title">' . $f['heading']['sfFilter'] . '</div>'; //We will only show the top 5 options, the rest get outputed in a "show more" div. They also outputed in order, with the most popular at the top. To achieve this we load them into a temporary array and then unset them once they have been outputed. $tmp = array(); $i = 0; foreach($f['options'] as $opt => $num) { $tmp[$i]['opt'] = $opt; $tmp[$i]['num'] = $num; } $limit = ($f['num_opts'] < 5 ? $f['num_opts'] : 5); //Output the first 5, or up to 5 if there are less than 5 filters. for($i = 0; $i < $limit; $i++) { $out = $this->_temp_first($tmp); for($j = 0; $j < $f['num_opts']; $j++) { if($i == $j || !isset($tmp[$j])) continue; if($tmp[$j]['num'] > $tmp[$i]['num']) $out = $j; } echo '<input type="checkbox" name="filter[]" id="filter' . $k . '" value="' . $tmp[$out]['opt'] . '" /> <label for="filter' . $k++ . '">' . $tmp[$out]['opt'] . (@$showfilternum ? '(' . $tmp[$out]['num'] . ')' : '') . '</label> <br clear="all" />'; unset($tmp[$out]['opt']); //WORKS FINE unset($tmp[$out]['num']); //WORKS FINE unset($tmp[$out]); //THROWS 500 ERROR?! } //Output the rest in a hidden div. if($f['num_opts'] > 5) { echo '<div class="filter-more" id="filter-more-' . $f['heading']['sfID'] . '">'; for($i = $i; $i < $f['num_opts']; $i++) { $out = $this->_temp_first($tmp); for($j = 0; $j < $f['num_opts']; $j++) { if($i == $j || !isset($tmp[$j])) continue; if($tmp[$j]['num'] > $tmp[$i]['num']) $out = $j; } echo '<input type="checkbox" name="filter[]" id="filter' . $k . '" value="' . $tmp[$out]['opt'] . '" /> <label for="filter' . $k++ . '">' . $tmp[$out]['opt'] . (@$showfilternum ? '(' . $tmp[$out]['num'] . ')' : '') . '</label> <br clear="all" />'; //unset($tmp[$out]); } echo '</div>'; } } } } /* Find the first temp */ private function _temp_first($tmp, $i = 0) { if(isset($tmp[$i])) return $i; else return $this->_temp_first($tmp, ++$i); } } Can someone please explain why simply unsetting that filter is throwing the error?
-
I managed to figure it out in the end. If anyone wants to see how here's the code: <?php /* Queue Manager */ function queue_manager($start_cat = 0) { $queue = array(); $num_queue = 0; $root = 0; for($i = 0; $i < count($this->cats); $i++) { if($this->cats[$i]['sectionID'] == $start_cat) { $root = $this->cats[$i]['rootSection']; } } if($root) { $this->output_product($start_cat); } else { $add_to_queue = $this->output_cat($start_cat); foreach($add_to_queue as $a) { $queue[$num_queue++] = $a; } } for($i = 0; $i < $num_queue; $i++) { $id = $this->cats[$queue[$i]]['sectionID']; $root = $this->cats[$queue[$i]]['rootSection']; if($root) { $this->output_product($id); } else { $add_to_queue = $this->output_cat($id); foreach($add_to_queue as $a) { $queue[$num_queue++] = $a; } } } } /* Output Category */ function output_cat($top) { echo '<div class="admin-tree-container">' . PHP_EOL; $output_buffer = array(); $return = array(); for($i = 0; $i < count($this->cats); $i++) { if($top == $this->cats[$i]['sectionID']) { echo '<div class="admin-tree-container-title">' . $this->cats[$i]['sectionWorkingName'] . '</div>' . PHP_EOL; } elseif($top == $this->cats[$i]['topSection']) { $return[] = $i; $li = '<li id="id' . $this->cats[$i]['sectionID'] . '"><span class="admin-tree-cat'; if($this->cats[$i]['sectionDisabled']) { $li .= ' cat-disabled'; } if($this->cats[$i]['rootSection']) { $li .= ' cat-root'; } $li .= '" id="id' . $this->cats[$i]['sectionID'] . '">' . $this->cats[$i]['sectionWorkingName'] . '</span></li>'; $output_buffer[] = $li; } } echo '<ul>' . PHP_EOL; foreach($output_buffer as $o) { echo $o . PHP_EOL; } echo '<li class="sort-button sort-cancel" id="btnid' . $cat . '">+ Add new product</li></ul></div>' . PHP_EOL; return $return; } /* Output Product */ function output_product($top) { echo '<div class="admin-tree-container">' . PHP_EOL; for($i = 0; $i < count($this->cats); $i++) { if($top == $this->cats[$i]['sectionID']) { echo '<div class="admin-tree-container-title">' . $this->cats[$i]['sectionWorkingName'] . '</div>' . PHP_EOL; $i = count($this->cats); } } echo '<ul>' . PHP_EOL; for($i = 0; $i < count($this->prods); $i++) { if($top == $this->prods[$i]['pSection']) { echo '<li class="sort-cancel" id="id' . $this->prods[$i]['pID'] . '"><span class="admin-tree-prod'; if(!$this->prods[$i]['pDisplay']) { echo ' prod-disabled'; } if(!$this->prods[$i]['pSell']) { echo ' prod-not-sell'; } echo '" id="id' . $this->prods[$i]['pID'] . '">' . $this->prods[$i]['pName'] . '</span></li>' . PHP_EOL; } } echo '<li class="sort-button sort-cancel" id="btnid' . $cat . '">+ Add new product</li></ul></div>' . PHP_EOL; }
-
Hey guys, I have a bunch of categories and products in a tree format. For several reasons I need to restructure them from the tree format into a flattened format. I have 2 arrays, one full of categories, the other products. Each category and product has an ID, and a reference to it's parent's ID. The top level categories reference to 0. What I need is to have the categories output like thus: Top Level Categories: Category 1 Category 2 Category 1 Sub category 1 Sub category 2 Category 2 Sub category 3 Sub category 4 Sub category 1 Product 1 Product 2 Sub category 2 Product 3 Product 4 Sub category 3 Product 5 Product 6 Sub category 4 Product 7 Product 8 There could be an unlimited number of sub categories within categories before we get to products, so this needs to be done through a function, however I cannot for the life of me think how to do this. Initially I thought about using 2 arrays, a buffer of categories outputted, and a queue of categories to be outputted, but quickly realised that when I go more than 2 layers deep I can't keep track of the queue's properly.
-
Nobody have any suggestions or solutions?
-
Hello, I have an interesting situation and I cannot determine what is wrong. I am trying to build a drag and drop category and product list. I am using jQuery UI's sortable function to do so. I am using the latest versions of jQuery and jQuery UI. The lists are laid out as so: <ul> // Top level categories <li>TLC 1 <ul>Sub Category 1 <li>Cat A</li> <li>Cat B</li> </ul> </li> <li>TLC 2 <ul>Sub Category 2 <li>Cat C</li> <li>Cat D</li> </ul> </li> </ul> My javascript is thus: $(".admin-tree-sort-cat").sortable({ cancel: ".sort-button", connectWith: ".admin-tree-sort-cat", update: function(event, ui) { var ids = $(this).sortable("toArray").toString(); $.ajax({ type: "POST", url: "ajax/tree_order.php", data: { ids: ids, cat: "1" }, success: function(data) { alert(data); } }); }, placeholder: "ui-state-highlight" }); Dragging from sub-category to top level category works fine. Dragging from sub-category to sub-category works fine. However dragging from top level category to sub-category only triggers the update on the top level category, not the sub-category, which means the change doesn't get saved. Why would dragging from sub-category to tlc work but not the other way round? Firebug shows no errors and all the correct ajax calls, except for the missing one when dragging from tlc to sub cat.
-
I guess it would have to come down to a choice on what is more necessary, security or usability. It's always a battle between the two. In my experience, in commercial sites, security will pretty much always win out. Maybe I don't have as much experience as you (I don't know your background) and haven't had a project where usability trumps security.
-
If you've fixed that, and any other potential mistakes, then you should post your latest code here for us. If you add alert(data); to the success function what do you get?
-
Seems I misread your post and overlooked the part where you mentioned getting the difference between a mismatched password and a user not existing. For that you're right, it would require 2 statements and I apologise for saying you were wrong. However that said, doing that is a security risk. It allows users to find out valid usernames. You shouldn't ever do that.
-
I just noticed your Ajax statment, you're not telling the HTML to update. You need to adjust you success statement to: success: function(data) { console.log("dat"); $('.classofdivtobeupdated').html(data); }
-
I used the wrong word then, Query not select. You're still hitting the DB. Whatever word, query, select, it's still 1.