-
Posts
24,609 -
Joined
-
Last visited
-
Days Won
832
Everything posted by Barand
-
try using different variable names for the input and the array.
-
For now, from your current query, I'll assume the data looks something like this +------------------------------------------------+ +-----------------------------------+ | Table bids | | Table bid_incrementss | +--------+---------+-------+---------------------+ +------------+----------+-----------+ | bid_id | item_id | price | created_timestamp | | price_from | price_to | increment | +--------+---------+-------+---------------------+ +------------+----------+-----------+ | 1 | 3 | 10.00 | 2015-08-29 12:18:56 | | 0.00 | 9.99 | 2.00 | | 2 | 2 | 20.00 | 2015-08-29 12:18:56 | | 10.00 | 19.99 | 3.00 | | 3 | 4 | 30.00 | 2015-08-29 12:18:56 | | 20.00 | 29.99 | 4.00 | +--------+---------+-------+---------------------+ | 30.00 | 99.99 | 5.00 | +------------+----------+-----------+ using this query SELECT CASE WHEN b.price IS NULL THEN (SELECT MAX(increment) FROM bid_increments) ELSE (b.price + bi.increment) END AS `minimum_bid` FROM bid_increments bi LEFT JOIN bids b ON b.price BETWEEN bi.price_from AND bi.price_to AND b.item_id = 1 ORDER BY item_id DESC LIMIT 1 the results are item_id = 1 item_id = 2 item_id = 3 item_id = 4 +-------------+ +-------------+ +-------------+ +-------------+ | minimum_bid | | minimum_bid | | minimum_bid | | minimum_bid | +-------------+ +-------------+ +-------------+ +-------------+ | 5.00 | | 24.00 | | 13.00 | | 35.00 | +-------------+ +-------------+ +-------------+ +-------------+
-
php encode to json help with morris.js please
Barand replied to bambinou1980's topic in PHP Coding Help
Your first task is create a select query with the correct syntax instead of the rubbish above. Look at the order of the statement parts in your other select queries that you have posted, and the use of important keywords, like WHERE. When you process the query, store in an array then json_encode() the array. What will you show on the graph when you have multiple values for the same date? -
Can we see some data so we know what we are dealing with?
-
Instead of just resurrecting a five year old topic, why don't you read it too. The answer is there.
-
Parse error: syntax error, unexpected 'INSERT' (T_STRING)
Barand replied to kjetill6's topic in PHP Coding Help
$info6 = "skap1'; ^ ^ | | notice something odd? -
How do you define constants that have been posted from a form?
Barand replied to rocky48's topic in PHP Coding Help
You get that error because the string index "color" is not in quotes $color = $_POST[color]; should be $color = $_POST['color']; Because you haven't used quotes, php thinks it is a defined constant. When it cannot find the definition it throws the error. -
$email is a string value and therefore needs to be in single quotes in your queries "INSERT INTO betakey (Mail) VALUES ('$Mail')" "SELECT (Mail) FROM betakey WHERE Mail = '$Mail' " It would make more sense to do the check with the SELECT query before inserting. You could just use the INSERT query if you define email as unique and do away with the select. Then, if you get a duplicate key error, you know it already existed.
-
$result = $db->query("SELECT something FROM somewhere");
-
MSSQL If select returns row then run update sql statement
Barand replied to kat35601's topic in PHP Coding Help
If you are using ODBC, why suddenly attempt to use mssql_num_rows() instead of odbc_num_rows()? -
try $SQL = " SELECT sr.review_id, sr.review_date, sr.review, sr.reviewer_name, sr.reviewer_surname, r.rating_name, r.rating FROM school_reviews sr LEFT JOIN ratings r USING(review_id) WHERE sr.active = 1 AND sr.is_deleted = 0 AND sr.school_id = $school_id ORDER BY sr.review_date DESC"; $q->query($DB,$SQL); // store results of query in array $reviews = array(); $totals = array(); // ADDED $ratingsCount = 0; // ADDED while($row = $q->fetch_assoc()) { // get the review id $id = $row['review_id']; // group reviews by review id if(!isset($reviews[$id])) { $reviews[$id] = array( 'message' => $row['review'], 'date' => $row['review_date'], 'name' => $row['reviewer_name'], 'surname' => $row['reviewer_surname'], 'ratings' => array() ); } // group ratings by review id $reviews[$id]['ratings'][] = array( 'name' => $row['rating_name'], 'value' => str_repeat('* ', $row['rating']) #'value' => '<img src="/en/images_v2/ratings/star'.$row['rating'].'.png" />' ); // rating totals // ADDED $ratingsCount++; if (isset($totals[$row['rating_name']])) { $totals[$row['rating_name']] += $row['rating']; } else { $totals[$row['rating_name']] = $row['rating']; } } // loop over the reviews foreach($reviews as $review_id => $review) { // output review echo "<div class=\"schoolreview\">"; echo "<p><span class=\"right\">Posted On: " . $review['date'] . "</span>"; echo "Review by: ".$review['name']."<br />\n" . "</p>\n"; // output ratings list for each review echo "<ul class=\"ratings\">\n"; foreach($review['ratings'] as $rating) { echo "\t<li>" . $rating['name'] . ': ' . $rating['value'] . "</li>\n"; } echo "</ul>\n"; echo "<p>Review: " . nl2br($review['message']) . "</p>"; echo "</div>"; } // loop over the totals // ADDED $reviewCount = count($reviews); echo "Average ratings\n<ul>\n"; foreach ($totals as $name => $tot) { $avg = number_format($tot/$reviewCount, 2); echo "<li>$name : $avg</li>\n"; } echo "</ul>\n"; $overallAverage = number_format(array_sum($totals)/$ratingsCount, 2); echo "Overall average rating : $overallAverage";
-
What is the query that is being executed?
-
You don't close the category select until after you close the form. Move the </select> to a position before the submit button +-------> | ^ <input type='submit' name='submit' value='Search' ></br></br></br></center> | </form> | +--<-- </select>
-
An alternative would be only to get the file contents when there is a new id value $prevID = ''; foreach($result as $row){ // set replacement to be the string from db if ($row['id'] != $prevID { $str = file_get_contents("templates/staff.html", true); $prevID = $row['id']; } $replace = $row['content']; $srch = $row['location']; $str = $this->replace($srch, $replace, $str); }
-
You have to define for what you want the average eg average rating for each review SELECT review_id, AVG(rating) from tablename GROUP BY review_id average rating for each rating_name SELECT rating_name, AVG(rating) from tablename GROUP BY rating_name average for the whole table SELECT AVG(rating) from tablename
-
I gave you the answer to that:
-
JOIN and FOREIGN KEY are not mutually exclusive, it isn't a case of one or the other. +------------------+ +-----------------+ | reseller | | order | +------------------+ +-----------------+ | reseller_id PK | ---+ | order_id PK | | reseller_name | | | order_total | | reseller_surname | +---< | reseller_id FK | +------------------+ +-----------------+ In the tables above, reseller_id is is the primary key (PK) of the reseller table. It also appears in the order table to relate an order to the reseller. As it is the PK of one table appearing in another table it is, by definition, a FOREIGN KEY. When querying the tables you (normally) JOIN the tables using this PK --> FK relationship. You also have the option of applying FOREIGN KEY CONSTRAINTS on a table. If you do this enforces "referential integrity" so you cannot add an order with a reseller_id that does not exist in the reseller table, or you cannot delete a reseller record that has related orders in the order table.
-
Your attempt doesn't use the same tables SELECT dr.id as 'id', name FROM drive_rating dr LEFT JOIN drive_routes dro ON dro.rating = dr.id WHERE dr.id=? compared with SELECT *,(SELECT dc.id FROM drive_cargotype dc, drive_routes dr WHERE dc.id = dr.cargotype AND dr.id=? ORDER BY dr.id DESC LIMIT 1) as cargo FROM drive_cargotype";
-
The complex part was combining the keywords. There is no combining required in the latest requirement of yours. Anyway, here's the code with keywords sorted by count, as the info required was in the arrays. <?php $narray[]="Trump denounces violence after supporters beat Mexican man"; $narray[]="Doyle: What my dad could teach Donald Trump"; $narray[]="Bush slams Trump, defends using anchor babies"; $narray[]="Coming up Trumps: could a British TV star do a Donald and enter politics?"; $narray[]="Watch Rachel Maddow Explain Donald Trump’s ‘Genius’ Campaign on Tonight Show"; $narray[]="Trump touts making Time cover while taking heat over attack"; $narray[]="First Draft: Today in Politics: Rivals Can No Longer Ignore Donald Trump’s Long Shadow"; $narray[]="Donald Trump insists he’s conservative"; $narray[]="GOP candidates hold dueling town halls"; $narray[]="New York City has no way to fire Donald Trump"; $narray[]="Donald Trump pushes birthright citizenship to forefront of political debate"; $narray[]="Jeb Bush takes fight to Donald Trump in N.H."; $narray[]="Rand Paul explains why he wants to stop ‘birthright citizenship’"; $narray[]="Trump attacks Facebook over foreigners"; $narray[]="Donald Trump tops GOP field in Florida, Pennsylvania, second in Ohio"; $narray[]="Donald Trump draws New Hampshire town hall crowd wild; jabs Jeb Bush"; $narray[]="While in Vegas, O’Malley makes an appearance in front of Trump’s hotel"; $narray[]="Trump’s immigration plan has GOP rivals on edge"; $narray[]="Donald Trump calls out Mark Zuckerberg on immigration"; $narray[]="Deny citizenship to babies illegal immigrants in US: Donald Trump"; $narray[]="Donald Trump takes a break from the campaign trail to join a long list of celebrities to perform jury duty"; $narray[]="Trump: Deny citizenship to babies of people illegally in US"; $narray[]="Trump Says He Would Deport Illegal Immigrants"; $narray[]="From campaign to court: Trump reports for jury duty in NYC"; $narray[]="Donald Trump says he will ‘deport millions of illegal immigrants’"; $narray[]="Trump outlines immigration specifics"; $narray[]="Donald Trump to Iowa boy: ‘I am Batman’"; $narray[]="Trump blunt but vague: No birthright citizenship, millions of illegal immigrants ‘have to go’"; $narray[]="Trump: end ‘birthright citizenship’"; $narray[]="Trump: Deport children of immigrants living illegally in US"; $narray[]="DNC blasts Donald Trump , Jeb Bush for comments about women"; $narray[]="Trump says would raise visa fees to pay for Mexican border wall"; $narray[]="What does Donald Trump think of immigrants, Saudi Arabia and the Iran nuclear deal?"; $narray[]="Donald Trump Releases Plan to Combat Illegal Immigration"; $narray[]="Donald Trump releases his immigration policy on his GOP presidential campaign website"; $narray[]="Donald Trump warns that Iran deal will lead to Nuclear Holocaust"; $narray[]="Trump details domestic, foreign policies, answers critics, matches fellow challengers"; $narray[]="Donald Trump’s legacy of luxury"; $narray[]="Clinton defends, Trump attacks Saturday at the high-profile Iowa State Fair"; $narray[]="Donald Trump says he would deport all illegal immigrants as president"; $narray[]="Donald Trump breaks the rules at the Iowa State Fair"; $narray[]="Thanks, Donald, but I don’t want to be ‘cherished’ | Barbara Ellen"; $narray[]="Front-runners skirt the soapbox"; $narray[]="Hillary Clinton, Donald Trump and the Trumpcopter descend on the Iowa State Fair"; $narray[]="Op-Ed Columnist: Introducing Donald Trump, Diplomat"; $narray[]="Trump forced to break from campaign trail for jury duty, skipped five summonses since 2006"; $narray[]="Donald Trump forced to take break from campaign trail for jury service"; $narray[]="Tables turned on Trump’s chief tormentor"; $narray[]="Donald Trump will serve jury duty in NYC next week"; $filtered = filter_my_array($narray); // keywords only array $keywords = []; $kwindex = index_keywords($filtered, $keywords); // index of keywords // // find items with no keywords // $otheritems = []; foreach ($filtered as $k=>$v) { if (count($v)==0) $otheritems[] = $k; } // // create output of the indexed lists // // rearrange key words by desc no of occurences / alpha sequence $countedKeywords = []; foreach ($keywords as $kw => $n) { $countedKeywords[$n][] = $kw; } $output = ''; krsort($countedKeywords); foreach ($countedKeywords as $n => $kws) { sort($kws); foreach ($kws as $kw) { $output .= "<h4>$kw<span class='count'>({$n}x)</span></h4><ul>"; foreach($kwindex[$kw] as $i) { $output .= "<li>" . str_ireplace($kw, "<span class='hi'>$kw</span>", $narray[$i]) . "</li>\n"; } $output .= "</ul>\n"; } } if (count($otheritems) > 0) { $output .= "<h4>Non-keyword items</h4><ul>"; foreach ($otheritems as $i) { $output .= "<li>{$narray[$i]}</li>\n"; } $output .= "</ul>\n"; } /******************************************************************************* * helper functions ********************************************************************************/ function filter_my_array($array) { // reduces the lines of text to arrays of the keywords in the line $results = []; foreach ($array as $k => $str) { $str = no_punc($str); $a = array_filter(explode(' ', $str), 'remove_noise'); $results[$k] = $a; } return $results; } function remove_noise($x) { $stopWords = array('about','an','and','are','as','at','be','by','com','de','en','for','from', 'how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where', 'who','will','with','und','the','www','donald','trump'); return strlen($x) > 3 && !in_array(strtolower($x), $stopWords); } function index_keywords($array, &$kwords) { // gets the line numbers containing each keyword $results = []; foreach ($array as $k => $kwarr) { foreach ($kwarr as $kw) { $results[$kw][] = $k; if (isset($kwords[$kw])) { ++$kwords[$kw]; // count keyword usage } else { $kwords[$kw]=1; } } } return $results; } function no_punc($str) { $allow = array_merge([32,39], range(ord('a'), ord('z')), range(ord('0'), ord('9'))); $k = strlen($str); $res = ''; $str = strtolower($str); for ($i=0; $i<$k; $i++) { if (in_array(ord($str[$i]), $allow) ) { $res .= $str[$i]; } else $res .= ' '; } return $res; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Keyword Index</title> <style type='text/css'> .hi { font-weight: 700; color: red; } .count { font-weight: 100; color: #f44; } </style> </head> <body> <?=$output?> </body> </html> Bye.
-
That is a completely different output from the problem you initially posed. Are you deliberately wasting my time?
-
How can i write a query: select from table where a pair of record is not there?
Barand replied to colap's topic in MySQL Help
two medium-sized tables mysql> SELECT COUNT(*) FROM tablea; +----------+ | COUNT(*) | +----------+ | 11956 | +----------+ mysql> SELECT COUNT(*) FROM tableb; +----------+ | COUNT(*) | +----------+ | 654071 | +----------+ Look for missing dates mysql> SELECT COUNT(DISTINCT thedate) as dates -> FROM tablea A -> LEFT JOIN tableb B -> ON A.thedate = B.DATA_COMPETENZA -> WHERE b.DATA_COMPETENZA IS NULL; +-------+ | dates | +-------+ | 10525 | +-------+ 1 row in set (0.05 sec) mysql> SELECT COUNT(DISTINCT thedate) as dates -> FROM tablea A -> WHERE NOT EXISTS -> ( -> SELECT * FROM tableb -> WHERE DATA_COMPETENZA = A.thedate -> ); +-------+ | dates | +-------+ | 10525 | +-------+ 1 row in set (0.08 sec) -
You need to set the random value inside the loop, not just once. while ($Health > 0) { $LostHealth = rand(1,10); $Health = $Health - $LostHealth; echo "<p>A Zombie just hit you doing <strong>$LostHealth</strong> damage!</p>"; echo "<p>You now have $Health HP</p>"; }
-
$prevkey = ''; foreach ($flavors as $k1 => $arr) { foreach ($arr as $k2 => $v ) { if ($v=='') { if (isset($flavors[$prevkey][$k2])) { $flavors[$k1][$k2] = $flavors[$prevkey][$k2]; } } } $prevkey = $k1; } echo '<pre>',print_r($flavors, true),'</pre>'; Sorry, I had $prevkey = $k1; in the wrong place.