-
Posts
24,338 -
Joined
-
Last visited
-
Days Won
795
Community Answers
-
Barand's post in Pull up last entries on a product inventory table was marked as the answer
SELECT product
, MAX(timestamp) as lastsale
FROM inventory
GROUP BY product
ORDER BY lastsale DESC
LIMIT 4 -
Barand's post in rotating letters in a dynamic image was marked as the answer
try
$chars = '16849'; $im = imagecreate(500,100); $bg = imagecolorallocate($im,0,0,0); $fg = imagecolorallocate($im,0,0xFF,0xFF); for ($c=0; $c<5; $c++) { $angle = $c*10; $ch = $chars[$c]; imagettftext($im, 60, $angle, $c*100+10, 90, $fg, 'c:/windows/fonts/Arial.ttf', $ch); } header("Content-type: image/png"); imagepng($im); imagedestroy($im);
-
Barand's post in Divide by zero warning was marked as the answer
Setting a variable $return_value does not return the value.
function get_query_value ($con,$query){ $con->real_query($query); $result=$con->use_result(); $row=$result->fetch_row(); $result->close(); return $row[0]; //<---- return the value } @ginerjm - the function is closing the result set, NOT the connection.
-
Barand's post in storing a resized image on the server was marked as the answer
imagepng
imagepng($dst, 'path/to/file'); -
Barand's post in get the serial number from the file name was marked as the answer
Both these will do it
$serialno = strstr($filename, '_', true); // get chars before the '_' $serialno = substr($filename, 0, 4); // get first four chars
-
Barand's post in problem with parsing xml was marked as the answer
$xml = simplexml_load_string($responseXml);
foreach ($xml->Messages->Message as $mes) {
echo $mes->MessageID . '<br>';
}
-
Barand's post in using php objects in strings was marked as the answer
I suspect it's because it uses "complex string syntax", just as $str = "ABC{$array[1][2]}"; requires the {..} whereas $str = "ABC$array[3]"; does not.
http://uk1.php.net/manual/en/language.types.string.php#language.types.string.parsing
-
Barand's post in phpexcel csv export 2 decimel place format was marked as the answer
Have you tried number_format()?
-
Barand's post in code challenge was marked as the answer
or
$data = file('hansford.txt',FILE_IGNORE_NEW_LINES); natsort($data); foreach ($data as $line) { echo "$line<br>"; } gives
#1A - Kessenich #1B - Adams #8 - Johnson #50 - Smith #100 - Sanders -
Barand's post in New PHP version is released! was marked as the answer
Consult the manual
http://php.net/manual/en/migration70.new-features.php
-
Barand's post in Return X words from string was marked as the answer
This closes off any current tags
$text = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam elementum ornare scelerisque.<br> <a href='xyz.com' target='_blank'>Vestibulum</a> iaculis mattis dui.</p> <p>Aliquam <i>scelerisque</i> sapien at tellus accumsan varius. <img src='a.jpg'> Fusce facilisis ullamcorper dapibus. Aliquam dignissim</p> <ul> <li>gravida</li> <li>dui eget</li> <li>aliquam</li> </ul> <p>Duis odio, semper eu sodales vel, sollicitudin eu enim. Cras tortor libero, pellentesque accumsan tempus in, ullamcorper nec augue. Mauris eu ipsum mauris, non imperdiet ipsum. In hac habitasse platea dictumst. Morbi ipsum mauris, tincidunt vitae pretium tempor, pretium a turpis. Nulla quis eros eu lorem aliquam congue non a nisl.</p>"; $voidtags = ['br','hr','img']; $keeptags = '<a><b><i><br><p><ul><ol><li><u><strong><emphasis>'; $limit = 30; $summary = limitText($text, $limit, $voidtags, $keeptags); echo $summary; function limitText($text, $limit, $voidtags, $keeptags) { $result = ''; $p=0; $tags=[]; $currtag = ''; $words = 0; $intag = $inword = 0; $text = strip_tags($text, $keeptags); $len = strlen($text); while ($p<$len) { $c = $text[$p]; switch ($c) { case '<': if ($inword) { $inword = 0; $words++; if ($words > $limit) break 2; } $intag = 1; break; case '>': if ($intag && $currtag != '') { if (!in_array($currtag, $voidtags)) $tags[] = $currtag; $currtag = ''; } $intag = 0; break; case '/': if ($intag) { array_pop($tags); do { $result .= $c; } while (($c=$text[++$p]) !='>'); $intag = 0; } break; case "\n": case "\t": case ' ': if ($inword) { $inword = 0; $words++; if ($words >= $limit) break 2; } elseif ($intag) { $tags[] = $currtag; do { $result .= $c; } while (($c=$text[++$p]) !='>'); $intag = 0; } break; default: if ($intag) { $currtag .= $c; } else $inword = 1; break; } $result .= $c; ++$p; } while ($t=array_pop($tags)) { $result .= "</{$t}>"; // close any open tags } return $result; } results
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam elementum ornare scelerisque.<br> <a href="xyz.com" target="_blank">Vestibulum</a> iaculis mattis dui.</p> <p>Aliquam <i>scelerisque</i> sapien at tellus accumsan varius. Fusce facilisis ullamcorper dapibus. Aliquam dignissim</p> <ul> <li>gravida</li> <li>dui</li></ul> -
Barand's post in Using dropdown as it to update record was marked as the answer
Try $_POST['aid'] instead of $_GET.
-
Barand's post in Nonomonotone mysql queries (Using NOT EXISTS) was marked as the answer
SELECT drinker
FROM frequents
WHERE bar IN (
SELECT bar
FROM frequents
WHERE drinker = 'Joe'
)
GROUP BY drinker
HAVING COUNT(bar)=1;
+---------+
| drinker |
+---------+
| Erik |
| Herb |
| Jesse |
| Justin |
| Mike |
| Vince |
+---------+
-
Barand's post in Help for a PHP select script for my website was marked as the answer
Do the calculation in the query
SELECT receptie.id , receptie.marca_tel , receptie.model , receptie.data_primire , articole_service.pret_sol , articole_service.pret_achizitie , articole_service.pret_sol - articole_service.pret_achizitie as profit -- add this FROM receptie inner join articole_service on receptie.id = articole_service.id_receptie then output $row['profit']
-
Barand's post in Join Multiple table was marked as the answer
I'd make a couple of changes
move the WHERE condition to the JOIN condition for the answer table use a LEFT join for the txtanswer table SELECT question.*, question_options.*, answer.*,txtanswer.* FROM question LEFT JOIN question_options ON question.question_id = question_options.question_id LEFT JOIN answer ON answer.option_id = question_options.qtn_option_id AND answer.empid = 'EMP8969' LEFT JOIN txtanswer ON txtanswer.qtn_option_id= question_options.qtn_option_id And don't use *s in the SELECT, specify the required fields.
-
Barand's post in the query is failing on a calculated field was marked as the answer
Using an aggregation function (SUM(), COUNT() etc) without specifying a GROUP BY column(s) will return a single row containing the aggregate for the whole selection. Values for the other non-aggregated selected columns are indeterminate (although usually from the first record in the set.
-
Barand's post in Check database name before running a query in phpMyAdmin was marked as the answer
I can think of only three ways
Only ever have a single database on your server Execute the statement "USE databasename" before every query. In INSERT, UPDATE and DELETE queries always prefix the table names with dbname (ie dbname.tablename). No damage is done with SELECT queries I suppose a fourth way is
be careful what you are doing -
Barand's post in IF in mysql WHERE clause was marked as the answer
Use a left join. If the is no matching se record then values from the se table will be null.
Use explicit A JOIN B ON condition syntax and not the FROM A,B WHERE.
You can use IFNULL() to check the status
SELECT ua.id , ua.security_key , ua.creation , ua.last_login , ua.f_name , ua.l_name , ua.email , ua.title , ua.org_name , ua.org_size , ua.manage_num , ua.manage_direct_num , ua.phone , IFNULL(se.status, 'No match') as status FROM user_accts AS ua LEFT JOIN sessions AS se ON se.author_id = ua.id AND se.status < ? WHERE ( $search_filter >= ? AND $search_filter <= ? ) ORDER BY $search_filter DESC -
Barand's post in Ajax not working good was marked as the answer
IDs have to be unique within a document. If you have multiple objects, use classname instead.
-
Barand's post in How do i put two int to one float like INT1,INT2 was marked as the answer
As this is a math help forum
$x=1; $y=1; $z = $x + $y/10; echo $z; -
Barand's post in Help With Getting MySQL Script To Work was marked as the answer
Sounds like you you have "modified_date" column in both tables. You have to specify which one to use
order_history.modified_date
or
research_queue.modified_date -
Barand's post in Add date offset to current code was marked as the answer
Create a dateTime object for each timezone. Format the time with 'P' format.
class Time_zone { private $regions = array( 'Africa' => DateTimeZone::AFRICA, 'America' => DateTimeZone::AMERICA, 'Antarctica' => DateTimeZone::ANTARCTICA, 'Artic' => DateTimeZone::ARCTIC, 'Asia' => DateTimeZone::ASIA, 'Atlantic' => DateTimeZone::ATLANTIC, 'Australia' => DateTimeZone::AUSTRALIA, 'Europe' => DateTimeZone::EUROPE, 'Indian' => DateTimeZone::INDIAN, 'Pacific' => DateTimeZone::PACIFIC ); public function generate_list() { $time_zones = array(); foreach ($this->regions as $name => $mask) { $time_zones[$name] = DateTimeZone::listIdentifiers($mask); } foreach ($time_zones as &$zones) { foreach ($zones as &$zstr) { $zn = new DateTimeZone($zstr); $t = new DateTime('now', $zn); $offset = $t->format('P'); $zstr = "[UTC/GMT $offset] $zstr"; } } return $time_zones; } } -
Barand's post in what is wrong with the query here? was marked as the answer
You haven't executed the prepared statement
-
Barand's post in Get All Records From Table one Joining other table was marked as the answer
When you LEFT JOIN to a table then any conditions regarding its data cannot be in the WHERE clause, they must be in the ON conditions in the join.
SELECT * FROM user as u LEFT OUTER JOIN event as e ON u.id = e.user_id AND e.event_date = '2016-07-05' AND e.event = 'Arrival' WHERE u.group_id = 6 GROUP BY u.id And don't use SELECT *. Specify the columns you want.