I made a simple query system through mySQL which is showing me 100 records and I fetch them into my game but I have probelm with the codes in PHP.
I want to have 5char space between each row So I have to use tab space (\t\t\t\t\t), But I have a problem with this current system (e.g If I have field with two diffrent string value 10char and 2char then use tab space to make space between them I get different results:
2Char string + 5char space = 7Char and 10Char string + 5Char space = 15Char
$query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 100";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_results = mysql_num_rows($result);
for($i = 0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo $i+1 . "-" . "\t\t Name: " .$row['name'] . "\t\t\t\t Device: " . $row['device'] . "\n \t\t Difficulty: " . $row['level']. "\t\t\t\t Score: " . $row['score'] . "\n\n";
}
Codes Output
1- Name: James Device: HTC OneX
Difficulty: Hard Score: 5760
2- Name: Erika_S Device: PC
Difficulty: Normal Score: 13780
...
My Desired Output
1- Name: James Device: HTC OneX
Difficulty: Hard Score: 5760
2- Name: Erika_S Device: PC
Difficulty: Normal Score: 13780
...
Tab in fact is one char, but displayed in the way that user want. When, for example, in IDE you choose 8 spaces for 1 tab you will get it. There's a fantastic concept called elastic tabstops, but it's only concept - so sad.
Conclusion: you can't do it what you described with tab.
What you can do:
Calculate needed spaces and hardcode with , but it's dirty and you shouldn't do this.
Use html tables
Instead of $row['...'] use sprintf("%-15s", $row['...']), but in each place you'll need to adjust the number (-15) to what's really needed
<?php
$s = 'monkey';
$t = 'many monkeys';
printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces
printf("[%010s]\n", $s); // zero-padding works on strings too
printf("[%'#10s]\n", $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters
?>
The above example will output:
[monkey]
[ monkey]
[monkey ]
[0000monkey]
[####monkey]
[many monke]
read more at http://www.php.net/manual/en/function.sprintf.php
if you can't use printf, you can easily create your own function that does something similar, and is enough for what you need:
function add_spaces($str, $total_len) {
return $str . substr(" ", 0, $total_len - strlen($str));
}
Related
Currently I have a form with 3 text areas.
It looks something like this
There is a button which sends the data to the PHP backend and creates a word document of this data and then downloads it for the user. This is a multi-line textarea obviously so the issue I am running into is that I am having trouble getting the proper formatting within this word document.
So for example if my textarea is setup like so:
Textbox1 Textbox2 Textbox3
File1 N/A 600
File2 Use Tab 1 1000
File3 Use Tab 2 200
I would expect the output in the word document to be just
File1 N/A 600
File2 Use Tab 1 1000
File3 Use Tab 2 200
But what I get is
File1
File2
File3 N/A
Use Tab 1
Use Tab 2 600
1000
200
In exactly this format. HOWEVER, if I only had one line (say the File1 line) it would actually output how I want it to. How can I setup my PHP string to format to my desired output?
My PHP is
if (strlen($multipleImports) > 0) {
$content = $content . "\n\n" . "Imports:\n ";
$content = $content . $multipleImports . $multipleImportsInfo . $multipleImportsCounts;
}
This is what ended up working for me:
$multipleImports = explode("\n",$multipleImports);
$multipleImportsInfo = explode("\n",$multipleImportsInfo);
$multipleImportsCounts = explode("\n",$multipleImportsCounts);
$content = $content . "\n\n" . "Imports:\n ";
$length = count($multipleImports);
for ($i = 0; $i < $length; $i++) {
$content = $content . $multipleImports[$i] . " " . $multipleImportsInfo[$i] . " " . $multipleImportsCounts[$i] . "\n ";
}
I need to search a text column for the occurrence of certain words in the column.
For instance as an example the column contents may look like:
KIT: TYPE WELD NECK ORIFICE FLANGE, CONSISTING OF TWO FLANGES WITH
JACK SCREWS BUT WITHOUT BOLTS AND GASKETS, RATING CLASS 600, RAISED
FACE, BORE TO MATCH .312 IN WALL, MATERIAL FORGED 304 STAINLESS STEEL
ASTM-A182 GRADE F304, SPECIFICATION: SP-50-13 REV: 1
Now the user needs to enter into a textbox for instance the following:
ASTM-A182 F304 WELD NECK
Currently I use this code:
$sql = "SELECT * FROM commtable WHERE MATCH (ldisc) AGAINST ('" . $ldisc . "' IN NATURAL LANGUAGE MODE);";
But most records returned (it returns hundreds of records) don't contain all the search terms entered in the text field.
How can I fine tune this full text search (or use another method) to give me a better result more closely to what was entered?
EDIT:
Table type:
EDIT 2:
It is working now, added this code:
if ($ldisc != "") {
$parts = explode(" ", $ldisc);
$tempSQL = "SELECT * FROM commtable WHERE MATCH (ldisc) AGAINST (" . "'";
for ($i = 0; $i < count($parts); $i++) {
$tempSQL = $tempSQL . '+' . $parts[$i] . ' ';
}
$tempSQL = $tempSQL . "' IN BOOLEAN MODE);";
$sql = $tempSQL;
$result = mysqli_query($con, $sql);
}
And changed the minimum word length to 1.
This question sounds like basically what you're looking for: MySQL fulltext search - Only results that contain all words
NATURAL LANGUAGE MODE by its nature returns approximate matches. To match all words in BOOLEAN MODE, you must add a + in front of every required word. For example,
MATCH (ldisc) AGAINST ('+ASTM-A182 +F304 +WELD +NECK' IN BOOLEAN MODE)
You'll have to split the input string and add the + signs. How to do that is left as an exercise to the programmer. =)
Change it to boolean mode
"SELECT * FROM commtable WHERE MATCH (ldisc) AGAINST ('" . $ldisc . "' IN BOOLEAN MODE);";
Another thing is to keep an eye on ft_min_word_length
I am sending values to a php script on the same server as a MySQL database. A php script sends the values to the database. However, somewhere along the path sometimes a value loses a decimal and I end up with an extremely high value.
I am monitoring PH and a few other things so the number is relatively low (between 4-12). Sometimes I end up with 720 instead of 7.20. Should this be corrected through a trigger? If so how? Or should it be handled on the PHP side? How can I not accept values or render them null when entering? Thank you for your time.
<!DOCTYPE html>
<html>
<body>
<h1></h1>
<?php
// Create connection
$con = mysqli_connect("localhost", "***", "***", "***");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$temp1 = ( $_GET["1"] * 9/5)+32;
$temp2 = ( $_GET["3"] * 9/5)+32;
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$sql = "INSERT INTO `ANALOG_SENSORS` (`TEMPERATURE`, `HUMIDITY`, `TEMPERATURE2`, `HUMIDITY2`,`PH`,`DATE_TIME`)
VALUES (" . $temp1 . ", " . $_GET["2"] . ", " . $temp2 . ", " . $_GET["4"] . ", " . $_GET["5"] . ",
NOW()- INTERVAL 7 HOUR)";
$result = mysqli_query($con, $sql);
}
</body>
</html>
Floating-point variables (float / double) should be handled as floating-point variable.
In MySQL and PHP, valid floating-point number are numbers with a dot as separator between integer and decimal part.
var_dump(floatval('13.37')) => double(13.37)
var_dump(floatval('13,37')) => double(13)
As you can see, with a coma, the decimal part is cut off, MySQL act exactly like this.
You have to be sure that your input data is formatted as is has to be.
$ph = str_replace(',', '.', $_GET['5']);
In a MySQL query, a coma is reserved character, if your data include a coma and isn't in a string (like in your case), the coma will be interpreted as separator, and add a new column to the insert query.
SELECT 1.2 => 1.2
SELECT 1,2 => [1, 2]
Also, make sure that your MySQL table fields' type are correctly set to float/double.
Regardless of what format the input is in, I am trying to use PHP to modify it, so the output is always consistent, rounds to two decimal places and has a dollar sign. My approach does not yield desired results all the time, I was wondering if someone can help me come up with a more robust solution?
Desired output:
$num = "$12.00"; //output $12.00
$num = "125"; //output $125.00
$num = "$300"; //output $300.00
$num = "17.5"; //output $17.50
$num = "1,777.5"; //output $1777.50
What I tried, however this does not work for all cases:
$num = "$" . str_replace("$", "", round($num,0)) . ".00";
I appreciate any advice! Many thanks in advance!
$num = "$" . str_replace("$", "", round($num,0));
In this line you round before you remove the $-sign. I must be the other way round:
$num = "$" . round(str_replace("$", "", $num),0);
EDIT: And to get the output you want, you might want to use number_format() instead of round()
$num = "$" . number_format(str_replace("$", "", $num),2, '.', '');
Newbie technical difficulty here.
Hope I can explain this clearly.
in My DB, I have snFreq, snFreq2, snFreqIV, snFreqTube, snFreqTrach, snFreqCath, snFreqWound, and snFreqOstomy.
I wrote an isset to check each column if data exist, then make a variable with preset text.
After all the checkpoints, I wanted to include this in a phrase, but don't want to include the "0" data. For some reason my output is still showing multiple commas with blank data.
Here's an example output:
SN FREQUENCY/DURATION: QD X 60 DAYS + 2 PRN VISITS FOR IV COMPLICATIONS, TUBE FEEDING COMPLICATIONS, , , , < = with extra commas?
In my DB:
snFreq = QD X 60 DAYS, snFreq2 = 1, snFreqIV = 1, and snFreqTube = 1, the rest are Null.
if(isset($rowPlanOfCare['snFreq'])){$snFreq = "SN FREQUENCY/DURATION: " . $rowPlanOfCare['snFreq'];}
if(isset($rowPlanOfCare['snFreq2'])){$snFreq2 = " + 2 PRN VISITS FOR ";}
if(isset($rowPlanOfCare['snFreqIV'])){$snFreqIV = "IV COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqTube'])){$snFreqTube = "TUBE FEEDING COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqTrach'])){$snFreqTrach = "TRACHEOSTOMY CARE COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqCath'])){$snFreqCath = "CATHETER CARE COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqWound'])){$snFreqWound = "WOUND CARE COMPLICATIONS";}
if(isset($rowPlanOfCare['snFreqOstomy'])){$snFreqOstomy = "OSTOMY CARE COMPLICATIONS";}
$snFrequency = $snFreq . $snFreq2 . implode(", ",array($snFreqIV, $snFreqTube, $snFreqTrach, $snFreqCath, $snFreqWound, $snFreqOstomy)) . "\n \n ";
Final Output should only show: SN FREQUENCY/DURATION: QD X 60 DAYS + 2 PRN VISITS FOR IV COMPLICATIONS, TUBE FEEDING COMPLICATIONS < == without the other commas.
I hope you can help me out with this issues. Thanks in advance! :)
Try this:
$snFrequency = $snFreq . $snFreq2 . implode(", ",array_filter(array($snFreqIV, $snFreqTube, $snFreqTrach, $snFreqCath, $snFreqWound, $snFreqOstomy))) . "\n \n ";