-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Mathematical formula to calculate spikes in line graph data?
Barand replied to D.Rattansingh's topic in PHP Coding Help
OK, I was feeling Geeky. Apparently yo would consider them spikes as they are outside the mean +- standard deviation (see attached resulting image) The code for anyone interested -
Use two separate queries in that case
-
Could PHP pass images via ajax to javascript?
Barand replied to D.Rattansingh's topic in PHP Coding Help
You could have a look at JQuery $(img).load(function() { alert('Image Loaded'); }); -
Could PHP pass images via ajax to javascript?
Barand replied to D.Rattansingh's topic in PHP Coding Help
Here's a simple working example. Two files, one to display and the other to create the GD image display_bar.html <html> <head> <script type='text/javascript'> function setSrc(v) { imgobj = document.getElementById("im"); imgobj.src = "img_bar.php?max=100&val="+v; } </script> </head> <body> <div style="background-color: black; padding: 25px; text-align: center;"> <img src="img_bar.php?max=100&val=0" id="im" /> </div> <br> Set bar percentage <br> <input type='button' name='btn1' value='40' onclick="setSrc(this.value);"> <input type='button' name='btn2' value='60' onclick="setSrc(this.value);"> <input type='button' name='btn3' value='80' onclick="setSrc(this.value);"> </body> </html> img_bar.php <?php /*************************** * Simple bar image * Author : Barand ****************************/ $im = imagecreatetruecolor(200,20); $bg = imagecolorallocate($im, 0xff,0x00,0x00); $bar = imagecolorallocate($im, 0x00,0xFF,0x00); $blk = imagecolorallocate($im, 0x00,0x00,0x00); $val = $_GET['val']; $max = $_GET['max']; $x = $max ? $val * imagesx($im) / $max : 0; imagefill($im,0,0,$bg); imagefilledrectangle($im,0,0,$x,imagesy($im),$bar); header("content-type: image/png"); imagepng($im); imagedestroy($im); ?> -
Rotate image string vertical, i.e. top to bottom
Barand replied to D.Rattansingh's topic in PHP Coding Help
image_rotate($im,180); would have the same effect. If you use imagettftext() you can specify any angle for the text. -
Check whether values from array exist in column...
Barand replied to Kristoff1875's topic in MySQL Help
You could check prior to insert like this $Selected = $_POST['Selected']; // eg "Box1,Box2,Box3" $Selected1 = explode(',', $Selected); $saleid = 456; // // check for duplicates // $boxlist = join ("','", $Selected1); $sql = "SELECT BoxID FROM Selected WHERE SaleID = $saleid AND BoxID IN ('$boxlist')"; $res = $db->query($sql); if ($res->num_rows > 0) { // dupes found echo "SaleID: $saleid<br>"; while ($row = $res->fetch_row()) { echo "Duplicated box: {$row[0]}<br>"; } } else { // OK to process inserts here } If you do try to insert a duplicate, the error number will be 1062 after attempting the insert -
Check whether values from array exist in column...
Barand replied to Kristoff1875's topic in MySQL Help
Add a UNIQUE key on (SaleID, BoxID) to your table -
Check whether values from array exist in column...
Barand replied to Kristoff1875's topic in MySQL Help
Yes, but it would depend on the rules for "exists already" is it a box can only ever be selected once? several users can select the same box but cannot select same box more than once? users can select the same box more than once but only once per saleID? etc -
Check whether values from array exist in column...
Barand replied to Kristoff1875's topic in MySQL Help
here's an example $Selected = $_POST['Selected']; // eg "Box1,Box2,Box3" $Selected1 = explode(',', $Selected); // assuming userid and sale id are numeric eg sample values $userid = 123; $saleid = 456; $data = array(); foreach($Selected1 as $Selection){ $data[] = sprintf ("(%d, %d, '%s')", intval($userid), intval($saleid), $db->real_escape_string($Selection)); } $sql = "INSERT INTO Selected (UserID, SaleID, BoxID) VALUES \n" . join (',', $data); echo $sql; /* outputs *** INSERT INTO Selected (UserID, SaleID, BoxID) VALUES (123, 456, 'Box1'),(123, 456, 'Box2'),(123, 456, 'Box3') */ -
How to use a variable function as a callback
Barand replied to CrimpJiggler's topic in PHP Coding Help
perhaps function X($matches) { // SOME CODE } $mo_diagram = 'X'; $bbcode = preg_replace_callback($pattern,$mo_diagram,$bbcode); -
You might also want to change the two occurences of WHERE user='$joysong=[friendid]' to WHERE user='$joysong[friendid]' Easier to spot when your query is structured instead of being strung out across the office $sql = "SELECT date, message, address FROM notifications WHERE nuser='$row[id]' UNION SELECT date, message, address FROM blogsnotification WHERE user='$joysong=[friendid]' UNION SELECT date, message, address FROM inmymind WHERE user='$joysong=[friendid]' UNION SELECT date, message, address FROM friendsnotification WHERE accountid='$row[id]' ORDER by date DESC LIMIT 0,15";
-
http://www.php.net/manual/en/function.mysql-fetch-array.php Look at the examples
-
you can get a list of weekdays with $dt = new DateTime('next weekday'); $di = DateInterval::createFromDateString('next weekday'); $dp = new DatePeriod($dt, $di, 9); foreach ($dp as $d) { echo $d->format('D jS F') . '<br>'; } /* RESULTS *** Mon 23rd December Tue 24th December Wed 25th December Thu 26th December Fri 27th December Mon 30th December Tue 31st December Wed 1st January Thu 2nd January Fri 3rd January */ edit: PHP >= 5.3
-
Here's a query that will process the buyers for each seller's items, tracking the amount still available for successive transactions. Price and quantity are maximised. The output gives the seller code and item code, buyer and the quantity by which the respective sold and bought columns should be updated. Hope it helps. SELECT seller, itemid, avail, required, qty, buyer FROM ( SELECT @req := Bamount - bought as `required` , @cumsold := CASE WHEN @prevseller=seller AND @previtem=itemid THEN @cumsold + @qtysold ELSE sold END as cumsold , @avail := Samount - @cumsold as avail , @qtysold := LEAST(@avail, @req) as qty , buyer , @prevseller := seller as seller , @previtem := itemid as itemid FROM ( SELECT S.itemid , S.amount as Samount , S.price , S.sold , B.amount as Bamount , B.price as Bprice , B.bought , B.playerHash as buyer , S.playerhash as seller FROM Buying AS B JOIN Selling AS S ON B.itemId = S.itemId WHERE (S.amount-S.sold)>=(B.amount-B.bought) AND B.price >= S.price AND S.sold < S.amount AND B.bought < B.amount ) as matched_transactions JOIN (SELECT @qtysold:=0, @cumsold:=0, @prevseller:=0, @previtem:=0, @req:=0, @avail:=0) as init ORDER BY seller, itemid, Bprice DESC, required DESC ) as main WHERE qty > 0 Sample output +--------+--------+-------+----------+------+-------+ | seller | itemid | avail | required | qty | buyer | +--------+--------+-------+----------+------+-------+ | 3 | 5 | 7 | 7 | 7 | 12 | | 5 | 1 | 4 | 3 | 3 | 100 | | 5 | 1 | 1 | 1 | 1 | 78 | | 7 | 5 | 1 | 1 | 1 | 4 | | 17 | 1 | 2 | 1 | 1 | 78 | | 17 | 1 | 1 | 2 | 1 | 13 | | 17 | 5 | 9 | 8 | 8 | 35 | | 17 | 5 | 1 | 7 | 1 | 12 | | 19 | 5 | 2 | 2 | 2 | 73 | | 20 | 5 | 2 | 2 | 2 | 73 | | 21 | 1 | 4 | 3 | 3 | 100 | | 21 | 1 | 1 | 1 | 1 | 78 | | 21 | 5 | 3 | 3 | 3 | 18 | | 23 | 1 | 5 | 3 | 3 | 100 | | 23 | 1 | 2 | 1 | 1 | 78 | | 23 | 1 | 1 | 2 | 1 | 13 | | 23 | 5 | 1 | 1 | 1 | 4 | +--------+--------+-------+----------+------+-------+
-
better way to structure table / write a query?
Barand replied to michaellunsford's topic in MySQL Help
Not only is it more efficient, it is also a whole lot better for you (or anyone else) reading the query at a later date as it helps to document the query. You can now see exactly what data the query needs to extract. -
Perhaps you should spend time reading the replies you have already had before we spend our time on any more
-
better way to structure table / write a query?
Barand replied to michaellunsford's topic in MySQL Help
Just use a column alias in the query. No need to rename them in the database. -
Consider CREATE TABLE search_example (category INT PRIMARY KEY,text VARCHAR(20)); INSERT INTO search_example VALUES (1, 'category'),(2, 'description'),(3, 'text'); This next query searches for column category = column text (as expected none found) mysql> SELECT category FROM search_example WHERE text = category; Empty set (0.00 sec) Now we put category string in quotes to tell SQL "this is a string value" mysql> SELECT category FROM search_example WHERE text = 'category'; +----------+ | category | +----------+ | 1 | +----------+ 1 row in set (0.00 sec) SQL can't know what you intended. It's up to you to be smart enough.
-
Don't put column names in quotes - it treats them as string values if you do that
-
Could be the lack of space between $grumble and LIMIT
-
Same thing. You are outputting $row['file1title'] and $f - which are the same if the title contains data.
-
When I emulate your second example with function fieldExists($fieldValue){ $trimValue = trim(str_replace(array(' ','\t','\n','\r','\0','\x0B'), '', ($fieldValue))); return empty($trimValue) ? false : $trimValue; } $xml = ''; $row= array ( 'file1Path' => 'something', 'listingID' => '1234' ); $f = fieldExists($row['file1Path']); $xml .= "\t<file1Path>" . ($f ? "www.someurl.com/Download.php?listID=" . ($row['listingID']) . "&no=1" : "No") . "</file1Path>\n"; I get <file1Path>www.someurl.com/Download.php?listID=1234&no=1</file1Path> As for your latest UTube problem, $f will contain $row['youTubeEmbed'] so you are outputting it twice. You need $f = fieldExists($row['youTubeEmbed']); $xml .= "\t<youTubeEmbed>" . ($f ? "<![CDATA[\"$f\"]]>" : "No") . "</youTubeEmbed>\n";
-
you missed a closing " after ListID= and it looks like you don't need the second $f $xml .= "\t<file1Path>" . ($f ? "www.someurl.com/Download.php?listID=" . ($row['listingID']) . "&no=1 $f" : "No") . "</file1Path>\n"; ^ ^ | | add remove It looks to me from your column names that your db table contains multiple columns like File1Path, File2Path,..., FileNPath and similarly with FIle1Tile etc. DON'T! Normalize the data. Store the data for the file in a separate table, one row per file. Only store where the file exists. That completely removes this problem you are having to check whether each field contains data or not. The XML also shouldn't have numbered tags (FIle1Path etc), better to have something like <Files> <File seq="1"> <Name>xxx</Name> <Title>yyy</Title> </File> <File seq="2"> <Name>www</Name> <Title>zzz</Title> </File> </Files>
-
In thatcase you do not need $row['d_name'] at all. You have the company in $comp $comp = $_GET['comp']; $sql="SELECT complain FROM complaint c WHERE c.d_name = '" . mysql_real_escape_string($comp) . "'"; //-run the query against the mysql query function $result=mysql_query($sql); //-count results $numrows=mysql_num_rows($result); echo "<p>" .$numrows . " results found </p>"; echo "<table>"; while($debtor=mysql_fetch_array($result) { echo "<tr>"; echo "<td>".$debtor['complain']."</td>"; echo "</tr>"; } echo "</table>"; echo "<br/><a href=\"companydetails.php?company=$comp\">See more details</a>";