Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Why are you making this har on yourself? Don't create new tables, simply have a column in a single table for the date. Then just query the single table using a between operator for the date. If you really feel you need multiple tables, then you will need to programatically determine all the tables you need to pull records from and use MERGE in your query to append the results from multiple tables. http://dev.mysql.com/doc/refman/5.1/en/merge-storage-engine.html
-
I have no idea what you are asking for. The document.write() doesn't contrl any images - it is simply writing a random image to the page - in a pretty sloppy way. If you need each image on the page in it's own position, just specify the images/layouts in the HTML. Not sure whay you want to achieve with JavaScript.
-
That is the foreign key which you need to use when you actually do the JOIN in the query. Not 100% sure if this is right since I'm not going to recreate your database to test with. If not, it will be close and we can tweak it once you describe what the results are. function fetch_categories() { mysql_connect(SQL_HOST_NAME, SQL_USER_NAME, SQL_PASSWORD) or die(mysql_error()); mysql_select_db(SQL_DATABASE) or die(mysql_error()); $query = "SELECT c.id, c.category_name, COUNT(c.id) as active_count FROM categories as c LEFT JOIN category_relations as cr ON cr.member_of = c.id AND cr.active = 1 GROUP BY c.id ORDER BY category_name"; $result = mysql_query($query)or die(mysql_error()); while($row=mysql_fetch_assoc($result)) { $listHTML .= "<li>";l $listHTML .= "<span class=\"numrows\">({$row['active_count']}</span>"; $listHTML .= "<a href=\"index.php?cat_ID={$row['id']}\">{$row['category_name']}</a>"; $listHTML .= "</li>\n"; } echo "<div id=\"category_list\">\n"; echo "<ul class=\"cat_list\">\n"; echo $listHTML; echo "</ul>\n"; echo "</div>\n"; }
-
Never run queries in a loop! It is a huge overhead on your server and will cause long page loads as you get more records. I'll take a look at the code and see about reducing it to one query and ficing the problem you are having.
-
jQuery to add value to hidden input and list after submit
Psycho replied to msaz87's topic in Javascript Help
I don't use JQuery, but here is a working example to add elements from a list, add them to a hidden input (I did not make the input hidden in the example for illustrative purposes), and the abilit to clear the values. For each list I used a single hidden input and delimited the values with a pipe (|) although you could use any character or even add a new hidden field for each item. <html> <head> <script type="text/javascript"> function addToList(listID) { var newInputObj = document.getElementById('add'+listID) if(newInputObj.value!='') { //Add value to hidden input var hiddenObj = document.getElementById('hidden'+listID); hiddenObj.value = hiddenObj.value + newInputObj.value + '|'; //Add value to list object var listObj = document.getElementById('list'+listID); var newListItem = document.createElement("LI"); newListItem.innerHTML = newInputObj.value; listObj.appendChild(newListItem); //Clear value and set focus on input newInputObj.value = ''; newInputObj.focus(); } } function clearList(listID) { var listObj = document.getElementById('list'+listID); //Remove all elements from the list while(listObj.childNodes.length>0) { listObj.removeChild(listObj.childNodes[0]); } //Clear the hidden input document.getElementById('hidden'+listID).value = ''; } </script> </head> <body> <table border="1"> <tr> <th>List 1</th><th>List 2</th><th>List 3</th> </tr> <tr> <td style="height:100px;" style="vertical-align:top;"> <ul id="list1"> </ul> </td> <td style="vertical-align:top;"> <ul id="list2"> </ul> </td> <td style="vertical-align:top;"> <ul id="list3"> </ul> </td> </tr> <tr> <td> <form onsubmit="addToList(1);return false" style="margin:0px;"> <input type="text" name="add1" /><br /> <button onclick="addToList(1);">Add</button> <button onclick="clearList(1);">Clear</button> </form> </td> <td> <form onsubmit="addToList(2);return false" style="margin:0px;"> <input type="text" name="add2" /><br /> <button onclick="addToList(2);">Add</button> <button onclick="clearList(2);">Clear</button> </form> </td> <td> <form onsubmit="addToList(3);return false" style="margin:0px;"> <input type="text" name="add3" /><br /> <button onclick="addToList(3);">Add</button> <button onclick="clearList(3);">Clear</button> </form> </td> </tr> <tr> <td> Hidden Input:<br /> <input type="text" name="hidden1" /> </td> <td> Hidden Input:<br /> <input type="text" name="hidden2" /> </td> <td> Hidden Input:<br /> <input type="text" name="hidden3" /> </td> </tr> </table> </body> </html> -
I don't use JQuery so I don't know if I am reading that code right, but I don't think you want to be attaching the validation to the onClick event of the submit button. Instead you should be attaching the validation to the submit event of the form. Otherwise, someone could submit the form using the enter key and the validation would not take place.
-
Using a variable as an argument's default value..
Psycho replied to Slips's topic in PHP Coding Help
When you pass values TO a function you are passing the values within the scope of where you called the function. So, if the function is called in the global scope (i.e. not in a function or within a different function where the variable has been declared 'global') then you are passing the global value - not the global variable. The function being called doesn't "know" where the value came from. However, once the function is called you are now running in a different scope. So, when you try to "use" a variable in the function it is within the scope of the function - unless you declare it to be global. I know of no way to declare a parameters in a function to be global. To be honest, this is a pretty worthless conversation. The whole process seems to make sense to me. Even so, multiple solutions have been provided where you can use a global value as the default in a function - you just can't do it within the parameter definition of a function. -
If there is no rhyme or reason to how the names are displayed or where they are displayed I don't see any solution for you. What you are asking for is something that humans can do easily, but is very difficult to program and almost impossible to get 100% correct. Possible solutiosn would include creating an expansive list of possible pre-fixes to the name (e.g. "Hi, im") or creating an even larger list of common names to search against.
-
Config.php Storing DB Username and password, Hacked??
Psycho replied to nightkarnation's topic in PHP Coding Help
You should not put anything with sensitive data within publc folders (i.e. available for users to access directly over the internet). Assuming everything is working as it should, anyone navigating to that file directly should get an empty page, but never assume there will never be problems. Instead put your config file one directory above the public directory. Then your PHP pages can access the config file via the local file path, but a user could never try to access it by entering the address into their browser. -
Using a variable as an argument's default value..
Psycho replied to Slips's topic in PHP Coding Help
Well, here is MY answer to the original question. The reason why you can't use a variable as the default value for a function is because of variable scope. A variable in a function is a separate object than the variable elsewhere in your code. You can define a variable as global within a function, but you can't do that in the parameter definition of the function. Here is how I would set up a function so that a variable will default to a global variable if the value is not passed in the function $foo = "bar"; function test($foo = false) { if(!$foo) { global $foo; } echo $foo . "<br />\n"; } test(); //Ouput: bar test('abc'); //Output abc Another solution would be to create your functions in a class and set a variable withint he class that can be used as the default. -
Good programming has nothing to do with the programming language and/or version used. If there is a good php programming book made in 2004, then it is still a good programming book. As Gizmola suggested, you should rely upon the manual to understand the available functions and how to use them.
-
I started using PHP about 6 years ago, but it is just a hobby. I work as a QA manager for a desktop software company. I used to work for a web development company for about 3 years prior to that doing mostly project management, but also dabbled in coding using VBScript, JavaScript and PostScript.
-
After looking at your code, I don't see anything that would lead me to believe that the checked items are used in multiple instances (i.e. each user has differnt checked options). If that is the case, just use one table that uses two columns: one for the option name and a second field for whether the option is checked. But, I have to assume your case is more complex than that or you wouldn't have posted here.
-
If you are getting a blank page, then there is apparently an error somewhere. I won't guarantee the error is not in the code I provided as I didn't test it. If you think the problem is in that loop, then comment out those lines and see if the page displaye (albeit, without the checkboxes). If it does, then you know the problem is in those lines.
-
Ok, after looking at the code briefly, I think you need to rewrite the form code. It is fine to create your checkbox IDs like this: id="venueID_<?php echo $row_venues_RS['venueID']; ?>" That makes sense since the IDs all have to be unique. BUt, the checkbox names (in this instance) should be set up as an array like this: name="venueID[]" Then you can just check $_POST['venueID'] to see if there are any checked fields instead of checking the names of all the submitted fields. And, even, better, the values of all the checked fields are passed as an array. So, first, use the following to create your checkboxes: while ($row = mysql_fetch_assoc($venues_RS)) { $checked = ($row['access']==1) ? ' checked="checked"' : ''; echo "<input type=\"checkbox\" name=\"venueID[]\" id=\"venueID_{$row['venueID']}\" value=\"{$row['venueID']}\"{$checked} />\n" echo "{$row['venueName']} {$row['venueID']}<br />\n"; } Second, your DELETE query has two WHERE clauses. I don't believe that is allowed. But, I think there is a better way to update the access records. First delete ALL access records for the selected user and venues. Then add back the ones which were checked. This will require that you pass the venueMasterID to the update script. Since you are using the venueMasterID to get the records to create the checkboxes, just create a hidden field with the venueMasterID. Plus, you should NEVER run queuries in a loop. When adding multiple records, just create one single query for the insert. I typically use an array for each record and do an implode to add them to a query. if (isset($_POST["Update"])) { //Process the submitted data $venueMasterID = mysql_real_escape_string(trim($_POST['venueMasterID'])); $userID = mysql_real_escape_string(trim($_POST['userID'])); $associationRecords = array(); foreach($_POST['venueID'] as $venueID) { if(ctype_digit($venueID)) { $associationRecords[] = "({$userID}, {$venueID})"; } } //Delete all current associations between user and the venues for the master venue $query = "DELETE FROM venue_user_access WHERE userID={$userID} AND venueID IN (SELECT venueID FROM venues WHERE venueMasterID = {$venueMasterID})"; $result = mysql_query($query); //Add associations for the selected venues if(count($associationRecords)) { $query = "INSERT INTO venue_user_access (userID, venueID) VALUES " . implode(', ', $associationRecords); $result = mysql_query($query); } }
-
I'm happy to help, but it would be good manners to provide at least the basics of what you want the code to do and what it is doing differently - especially explaining any errors you might be getting.
-
1. That is an improper use of selected within an OPTION tag. It should be selected="selected". 2. That code can be written much more efficiently. I will provide some code (see below), but it would be helpful if you stated if the prior years should be fixed (should start at 1998 as ras1986 did in his example) or if it should be dynamic (e.g. should include the previous 10 years - before January it will go back to 2000, but after Jan 1st it will only go back to 2001). Also, since 2010 is the default selected value I would "assume" that the list should be sorted in reverse order (in which case you don't need to use the selected parameter) 3. It is good practice to break up your logic (the core PHP code) from the presentation (the HTML). So, put the logic to determine the options at the top of your script and just output the results int he HTML. This makes your code much, much easier to maintain. The HTML code <html> <body> <form action="archivednews.php" method="post"> <select name="year" id="year"> <?php echo $yearOptions; ?> </select> <input type="submit" value="GO"> </form> </body> </html> PHP code to output from currentYear-10 to current year, autoselecting current year <?php $yearOptions = ''; for($currentYear=date('Y'), $year=$currentYear-10; $year<=$currentYear; $year++) { $selected = ($year==$currentYear) ? ' selected="selected"' : ''; $yearOptions .= "<option value=\"{$year}\"{$selected}>{$year}</option>\n"; } ?> PHP code to output from current year to currentYear-10 (i.e. reverse order). Current year will be selected by defaults since it is the first item in the list. <?php $yearOptions = ''; for($year=date('Y'), $lastYear=$year-10; $year>=$lastYear; $year--) { $yearOptions .= "<option value=\"{$year}\">{$year}</option>\n"; } ?>
-
Huh? I don't think you are understanding what I provided in that second example. The "access" field is a dynamically created field using an IF statement in the query - it does not require that there are corresponding records in the access table. Did you even try it? It will make your code much easier to write and more logical. The value for "access" is determined based on whether there was a record in the access table associating the user to the venue. If there was, then the value of access will be "1" (for true). If there was no corresponding record to associate the user with the venue (i.e. the result is null) then the value of "access" is determine to be "0" (false).
-
$cutoffTime = 44640; //Cutoff set to one month So, you are saying that people must submit thier number at least one month prior to the draw? According to your first post I thought the cutoff time was going to be one hour. If you want the cutoff to be a month I would have taken a different approach since a month can be anywhere from 28 to 31 days. I'd probably use strtotime(). $drawTime = mktime($20, $5, 0, $12, $25, $2010); //8:05PM on 25/12/2010 Well, you shouldn't have variable names that begin with numbers, but assuming those variable names are just for illustrative purposes and they would hold the value that is the same as the names, that is correct. And yes MySQL would be fine for holding "a lot" of records. It is a database, that's kind of what it's supposed to do. I'm not saying the code works though, you need to test it. Just set the draw time and the expiration so you have a 5 minute window to submit numbers. Ensure you can submit up until the cutoff and then get the expiration message afterward.
-
Ok, I don't know what happened. I was in a rush to get to a meeting, so I must have had a copy/paste error because I did validate my solution before I posted. Anyway, the problem is I left off the "LEFT" for the JOIN. Using a LEFT join tells the query to get all records from the left (i.e. first) table even if there is no matching record from the JOIN statement. In those cases the results from the JOINed table are null. By the way, you should only select the fields you need instead of using *. In fact, this is the query I would probably use: SELECT v.venueID, v.venueName, IF(va.userID=116, 1, 0) as access FROM `venus` as v LEFT JOIN venue_user_access as va ON v.venueID = va.venueID AND va.userID = 116 WHERE v.venueMasterID = 113 That will return a result set as follows (assuming the user 116 does not have access to "My Club" as you proposed in your last post) venueID | venueName | access 65 My Club 0 66 Club 2 1 Then just use the value of the access field to determine whether to check the checkbox or not.
-
Let me paraphrase what I understand before I provide a solution. On the page in question you have a venu master ID and a user ID. You want to list ALL the venus that match the venu ID and provide a checkbox for each. in addition, you want the checkboxes checked IF there is a matching record to associate the user to the particular venue. Here is the basic syntax for the query that you need: SELECT * FROM `venus` as v JOIN venue_user_access as va ON v.venueID = va.venueID AND va.userID = 116 WHERE v.venueMasterID = 113
-
Then why do you need the first IF statement? You could get the same results with the following: $sessionIndexes = array( 'reg' => 'alert', 'fail' => 'alertr', 'logged' => 'alert', 'failedlog' => 'alertr', 'logout' => 'alertr' ); foreach($sessionIndexes as $index => $divID) { if(isset($_SESSION[$index])) { echo "<div id=\"{$divID}\">{$_SESSION[$index]}</div>"; unset($_SESSION[$index]); session_destroy(); } }
-
The answer to your question was staring you in the face before you posted this. The very first stikied and locked post at the top of the PHP forum is titled "HEADER ERRORS - READ HERE BEFORE POSTING THEM". The answer you are looking for is there: http://www.phpfreaks.com/forums/php-coding-help/header-errors-read-here-before-posting-them/
-
I agree that isset() is probably the best way to check this, but adding all the vaues into one isset() will not give the intended results. When adding multiple values in isset, it will return TRUE only when all the values are true. In this case the OP wants to return true if one of the values isset. So, instead you could use: if(isset($_SESSION['reg']) || isset($_SESSION['fail']) || isset($_SESSION['logged']) || isset($_SESSION['failedlog'])) { However, if empty() is the correct test in this situation, then you could simplify the condition by concatenating the values as follows: if(!empty($_SESSION['reg'].$_SESSION['fail'].$_SESSION['logged'].$_SESSION['failedlog'])) {