Psycho
Moderators-
Posts
12,159 -
Joined
-
Last visited
-
Days Won
130
Everything posted by Psycho
-
The sine of 30 degrees is 0.5, but the JavaScript sine function [Math.sin()] uses Radians. To be honest, I don't recall what a Radian is exactly (been almost a decade since I took any advanced math classes), but using the calculator in Windows shows the sine of 30 radians to be -0.9880316240928618 (rounded), which is exactly what the javaScript code produces. Edit: http://math.rice.edu/~pcmi/sphere/drg_txt.html So to get the sine of degrees use the following (replace 30 with the degrees you need to calculate): document.write(Math.sin(30*Math.PI/180));
-
I think I understand you, but not totally. But, if you have multiple fields with the same name set as an array (i.e. fieldName[]) there is an easy enough way to reference them in JavaScript. When there are fields with the same name they are also an array within the javascript. In PHP the fields with the name "fieldName" would be referenced as an array in PHP as $_POST['fieldName']. However, in JavaScript, the array index includes the name WITH the brackets. document.form.elements['fieldName[]'] is the array of those field objects and document.form.elements['fieldName[]'][0] would be the first field object. Should be no problem referencing the fields you need accordingly
-
The following function will return a multidimentional array of every combination between array1 and array2. You could then do an array_rand to radomize the order of the combinations. Then get however many unique pairs that you need. function arrayJoin($array1, $array2) { $combinedArray = array(); foreach ($array1 as $value1) { foreach ($array2 as $value2) { $combinedArray[] = array($value1, $value2); } } return $combinedArray; }
-
To ensure the results are unique you could either create a new array of all the combinations or create a process that will keep track of the previous combinations and ensure there are no duplicates. The process I would use would depend on how big I expect those two arrays to be and how many combinations I expect to retrieve
-
Help with a generated select list being rendered where it should be
Psycho replied to Darkmatter5's topic in HTML Help
Ok, I just realized the the second block of code is apparently within an echo statement. That's the problem. You have an echo calling an echo. So, when the PHP is getting ready to process the echo in the lower block of code it has to call the function first. So, that function will perform it's echo before the echo which called it. The function need to "return" the output instead of echo'ing it. Here's an example of what I mean: function echoBar() { echo "bar"; } echo "foo" . echoBar(); //Output barfoo I have rewritten the function (below) to fix that problem and make it flow better. However, I would recommend some changes in the process itself. For example, you create an ID variable that is not used as the ID of the select list, instead it is used to determine the value from the query to be used in the options. I think that it would be much easier to pass queries to the function that will dynamically pull the needed data. For example if you have a table of colors and used color and color_id you could ensure that the values returned were in a generic format for this function SELECT color as name, color_id as label FROM colors Just prepare the queries for each table so the values are always returned as name and label (or whatever you want to use) New Function <?php function dropdown($name, $selectQuery, $default, $onchangeFunc) { $id = $name. '_id'; $onchange = (isset($onchangeFunc)) ? ' onchange="{$onchangeFunc}(this.value);" : ''; $options = "<option value=\"0\">--SELECT--</option>\n"; $query = mysql_query($selectQuery) or die(mysql_error()); while($row = mysql_fetch_array($query)) { $value = $row[$id]; $label = $row[$name]; $selected = ($value==$default) ? ' selected="selected"' : ''; $options .= "<option value=\"{$value}\"{$selected}>{$label}</option>\n"; } return "<select name=\"{$name}\" class=\"text_boxes\"{$onchange}>{$options}</select>\n"; } ?> -
Help with a generated select list being rendered where it should be
Psycho replied to Darkmatter5's topic in HTML Help
Look at the rendered HTML to see where the problem is. I suspect the problem may be due to this line in the function if(isset($onchange)) { echo " onchange='$onchange(this.value)'"; } The PHP is going to try and parse the "$onchange" as a PHP variable since you are using double quotes to define the string. But, ity may also be picking up some of the text in parens. -
If you want to learn more just google for tutorials on using JOINS in queries. Unfortunately tying to explain how to do it in a forum would be difficult because it would just take too much information to try and explain. Plus, I am by no means an expert in database queries, but for situations that are not strait-forward, I can typically get what I need after a little trial and error.
-
Had I known that the "work" was a fireign key in your table it would have made all of this MUCH easier and would have saved me a lot of time. Plus, now I'm guessing that the customer is a foreign key in the table as well. I'm really not up for taking another wack at this without being provided ALL the details.
-
OK, this works. It includes a query to get the totals for all name/work combinations - even those that don't exist, which makes displaying the results very simple. I have to believe there is a simpler way to do the query, but it works. <?php $db = mysql_connect('localhost', 'root', ''); mysql_select_db('test'); //Query the unique works to generate the headers $query = "SELECT DISTINCT work FROM `table` ORDER BY work"; $result = mysql_query($query) or die(mysql_error()); while($record = mysql_fetch_assoc($result)) { $works[] = $record['work']; } //Start the report table including headers $report = "<table border=\"1\">\n"; $report .= " <tr><th>Name</th><th>" . implode('</th><th>', $works) . "</th></tr>\n"; //Query the records $query = "SELECT t2.name, t2.work, COUNT(t1.work) as total FROM ( SELECT tt1.name, tt2.work FROM (SELECT DISTINCT name FROM `table`) tt1, (SELECT DISTINCT work FROM `table`) tt2) t2 LEFT JOIN `table` t1 ON t1.name = t2.name AND t1.work = t2.work GROUP BY t2.work, t2.name ORDER BY t2.name, t2.work"; $result = mysql_query($query) or die(mysql_error()); //Add the customer records $currentName = ''; while($record = mysql_fetch_assoc($result)) { if($currentName!=$record['name']) { if ($currentName!=false) { $report .= "</tr>\n"; } $currentName=$record['name']; $report .= " <tr>\n"; $report .= " <td>{$currentName}</td>\n"; } $report .= " <td>{$record['total']}</td>\n"; } $report .= " </tr>\n"; $report .= "</table>\n"; ?> <html> <body> <?php echo $report; ?> </body> </html> Here was my test data name | work =================== bob | oil change bob | brakes jane | brakes jim | oil change robert| alignment bob | oil change jane | brakes And here was the result Name | alignment | brakes | oil change ======================================= bob | 0 | 1 | 2 jane | 0 | 2 | 0 jim | 0 | 0 | 1 robert| 1 | 0 | 0
-
I know this can be done with a single query such that a "null" record will be created for each customer/work item that doesn't currently exist. This would greatly ease the process of displayng the records without a lot of complicated logic. I'm having some mixed results in joining the table to a sub query of just the distinct work types, but I'm missing something.
-
With all due respect Crayon, that query won't work. By doing a GROUP BY only on "work", you will group records from different customers. The GROUP BY needs to be "GROUP BY name, work"
-
The process will be more complex than a single query because not every customer will have used every service! The process will require two queries (one to get the unique work types in addition to the one ignace supplied) and then additional logic to be used when displaying the results to handle customer/work combinatinos that don't exist. Besides the query is wrong. I'll post some code in a few minutes
-
[SOLVED] identify an input as part of a checkbox group dynamically
Psycho replied to jug's topic in Javascript Help
You should just name your checkbox "groups" with the same name that will be treated as an array by the processing page. You can then also reference these "groups" within the javascript. Example: <input type="checkbox" name-"somefield[]" value="1"> <input type="checkbox" name-"somefield[]" value="2"> <input type="checkbox" name-"somefield[]" value="3"> These can be referenced via javascript like this: var formName = "searchform"; var formElements = document.getElementById(formName).elements; var checkBoxGroup = formElements['somefield']; var firstCheckbox = checkBoxGroup[0]; -
Yeah, for a minute there (well, at least a second or two) I thought I was missing something. Here are a couple quick functions you could use building upon what haku posted function allInArray($needleArray, $haystackArray) { //Returns true or false based on if all the values in the //needle array are found inthe haystack array return (count(array_diff($needleArray, $haystackArray)) == 0); } function someInArray($needleArray, $haystackArray) { //Returns false if no values are found in haystack array //or an array of the values found in the haystack array $found = array_intersect($needleArray, $haystackArray); return (count($found)==0) ? false : $found; } Not tested
-
For scenario 1 I would use array_intersect() and see if the resulting array count is > 0. If so, you know one or more of the values (5,8,23) exist in the target array.
-
My pleasure. And, yes, JOINS (as well as other database functions) are very cool s**t. That was why I asked if you "understood" JOINS, but I guess your answer was appropriate based on the fact that you had a general understanding but did not understand their true power. What I showed only scratches the surface of how to use a database - and I don't even consider myself very knowledgeable. If you have questions in the future, just as in this forum. It's better to get help from many people than just one as one person might not always have the best answer. You can always PM me on the forum, but I will probably tell you to just start a new post.
-
[SOLVED] Query DB and split results between two arrays
Psycho replied to siamiam's topic in PHP Coding Help
$result = mysql_query("SELECT DISTINCT city, state FROM exp_weblog_data"); while($row = mysql_fetch_array($result)) { echo $row['city'] . ", " . $row['state']; echo "<br />"; $cities[] = $row['city']; $states[] = $row['state']; } -
I'm making some gross assumptions on table and field names, but it would look something like this: SELECT k.NazivKom, k.KomitentID, k.Adresa, k.Telefon, k.E_mail, k.PostanskiBroj, k.MatBrKom, k.PorBrKom, AVG(t.someField) as avgValue FROM komitenti k LEFT JOIN tender t ON k.KomitentID = t.KomitentID GROUP BY k.KomitentID ORDER BY NazivKom ASC You would get the same results as before with an additional field called "avgValue" (or whatever you wanted to call it).
-
To be honest, I'm not sure what to make of your code above. Or that it even matters. All you need to do is use a JOIN, AVG and GROUP BY in the query for the actors and you will,in effect, be dynamically adding an "avg" column. Just give the AVG value a name and display it as you do any other field. But, do you only have one field in the movie table to associate the actor? How do you associate more than one actor to a movie?
-
Ok, my understanding is that the actor rating would consist of the average of the movie ratings for which the actor is associated with. Is that correct? If so, then you should not try to "store" that value because you can always get that value dynamically with the database query. The whole point of a relational database is to be able to dynamically extract data across tables. The example I gave was just an example based upon a "mock" database with two tables movies and actors. I can't provide any specifics in your situation because I have no clue as to your table structure. I'm actually kind of curious how you associate movies and actors if you don't have a third table for the associations. Also, not to be disrespectful, but do you understand what JOINS are and how they are used?
-
Well, when you say "if a number of values all exist within an array" the best answer may be array_diff(), array_diff_assoc(), array_intersect() or array_intersect_assoc(). Your question was a little too vague. Perhaps you could provide a real example of what you are wanting to accomplish.
-
Your code has the right logic, but there are just a few problems. 1. Unless you are needing the other fields, just query for the image. 2. You need to enclose the array index name within quotes. 3. You are only checking if the query returns a value for "image". You need to check if the value contains anything. I think that check might work if you are setting the value as NULL, but it won't work if the value is an empty string - which is what most people do 4. Unlsee you are storing the image as the complete html source to display the image (not recommended) you need to add the HTML tags to the path. here is how I would do it: <?php include "../link/log.php"; include "../link/dead.php"; $sql="SELECT img FROM players WHERE id='$id';"; $result=@mysql_query($sql, $con) or die(death($sql)); $u_row=mysql_fetch_array($result); //Determine the image to display if ($u_row['image'] != '') { $img = $u_row[image]; //Add the appropriate path } else { $img = "sword.jpg"; //Add the appropriate path } //Display the image echo "<img src=\"{$img}\">";
-
Hmm... I only took a cursory look at your code and I'm really not understanding what you are doing. I expect you are using the "_req_" as a lazy way of doing validation. Why? I would suggest you should explicitly state what fields need to be validated and what validation should be done. I notice on your form that EVERY SINGLE FIELD IS REQUIRED. That makes no sense to indicate all the fields as required on the form itself (You should do the appropriate validation on the receiving page, of course). But, your SELECT fields all have "valid" values. So it is impossible for the user to not have a valu in that field. Same goes for your radio groups - you have an option selected by default so the user can't not select an option. If you have a "--SELECT ONE--" or similar field in a select list that has no value or if you don't select a radio group option be default, then it would make sense to mark them as required. As for your second question, I would make groups of checkboxes an array by giving them the same name such as "fieldName[]". Then the receiving page will be able to access the array of values for a group using $_POST['fieldName']. Just do an implode() to convert the array to a string and add the commas.
-
Absolutely no reason to create a column in the actors table to store an average of the movie ratings he is associated with. That is the whole reason for a relational database. You can get the information in real time when you query the database. Trying to constantly update the average field is just a waste of time trying to do something manually that can be done automatically and will only lead to error in the long run. Here is a mock example of what the query would look like to grab the info for an actor along with the average of the movie ratings for the movies he is associated with. SELECT a.*, AVG(m.rating) as avg_rating FROM actors a LEFT JOIN movies . ON a.actor_id = m.actor_id WHERE a.actor_id = '$actorID' GROUP BY a.actor_id EDIT: Also you should not be storing the actor ID into the movies table. This is a many-to-many scenario - a movie may be associated with many actors and an actor may be associated with many movies. You should be using an intermediary table to associate actors and movies. It just needs two columns: movie_id and actor_id.
-
Those "codes" are necessary to display the text as formatted by the user using TinyMCE - that is what it does. If you do not want to show the text with any formatting then don't use TinyMCE. However, if you need to repurpose the user text for both formatted and nonformatted purposes, you could simply use the PHP function strip_tags(). http://us.php.net/strip_tags