I have some data on my db and I'm printing those to screen vertically but I want to print them on three different column. They can be in div, table etc.
For two column I found a solution.
If current row is even number, print location A
else location B
but if we want to print to more than two column how can we do that?
For example input must like that..
Name1 | Name2 | Name3
Name4 | Name5 | Name6
..... | ..... | .....
This way is much better than the previous I suggested. Took me a while to find it, but I knew I had done it somewhere!
echo '<table><tr>';
for ($i=0; $i < 40; $i++) {
foreach ($data as $el) {
echo '<td>' . $el . '</td>';
if ($i++ > 1 && $i++ % 3 == 0 ) echo '</tr><tr>';
}
}
echo '</tr></table>';
Related
I am trying to learn Laravel, so I apologize if my question is simple. I have 2 table (table1, table2) like this:
table1:
ID date time
1 1 1
2 4 2
3 5 3
table2:
ID V R
1 123 T
1 12 F
1 43 F
2 32 T
2 23 T
3 43 F
because I have 3 type of IDs (which could be more or less) I want to divide table2 into 3 tables using table1. like this:
table2_1: for ID:1
V R
123 T
12 F
43 F
table2_2: for ID:2
V R
23 T
23 T
table2_3: for ID:3
V R
43 F
I think I need somthing like this:
#foreach ($table1 as $t)
<table class="table">
{!! $t -> ID!!}
<thead>
<tr>
<th scope="col">R</th>
<th scope="col">V</th>
</tr>
</thead>
<tbody>
<!---Query result ---->
</tbody>
#endforeach
which in Query result I need a to select V and R from joining table1 and table2.
but I don'n know the exact code.
Any Idea how can I do this? Thanks in advance.
You may not even need to involve table1 here. Just query table2 with an ORDER BY clause, and then iterate the result set, turning out new HTML tables for each new ID value:
$result = DB::table('table2')
->orderBy('ID')
->get();
$prev_id = NULL;
foreach($result as $row) {
$curr_id = $row->ID;
if ($prev_id != NULL && $curr_id != $prev_id) {
echo "</table>";
}
if ($prev_id == NULL || $curr_id != $prev_id) {
$prev_id = $curr_id;
echo "<table class=\"table\" colspan=\"2\">";
echo "<tr><th scope=\"col\">V</th><th scope=\"col\">R</th></tr>";
}
echo "<tr><td>" . $row->V . "</td><td>" . $row->R . "</td></tr>";
}
echo "</table>";
I have some results from a mysql table that I would like to export, I am currently able to click a download link and download an xls but I would like to be able to run this via a cron job and have the weekly results email to me.
I have looked at doing this from Mysql and save it out as a csv directly.
However I am struggling with the SQL, the table format is as follows
btFormQuestions (some columns ommitted)
+-------+---------------+----------+-----------+
| msqID | questionSetId | Question | InputType |
|-------+---------------+----------+-----------+
| 1 | 123456 | Name | field |
| 2 | 123456 | Telephone| field |
| 3 | 123456 | Email | email |
| 4 | 123456 | Enquiry | test |
btFormAnswers
+-----+------+-------+-----------------+
| aID | asID | msqID | answer |
+-----+------+-------+-----------------+
| 1 | 1 | 1 | Sean |
| 2 | 1 | 2 | 0800 0 |
| 3 | 1 | 3 | se#te.com |
| 4 | 1 | 4 | Asking Question |
btFormAnswersSet
+------+---------------+---------------------+
| asID | questionSetId | created |
+------+---------------+---------------------+
| 1 | 123456 | 2013-04-30 11:07:55 |
The sql queries, I am currently using to get the information into PHP and into an array is as follows:
//get answers sets
$sql='SELECT * FROM btFormAnswerSet AS aSet '.
'WHERE aSet.questionSetId='.$questionSet.' ORDER BY created DESC LIMIT 0, 100;
$answerSetsRS=$db->query($sql);
//load answers into a nicer multi-dimensional array
$answerSets=array();
$answerSetIds=array(0);
while( $answer = $answerSetsRS->fetchRow() ){
//answer set id - question id
$answerSets[$answer['asID']]=$answer;
$answerSetIds[]=$answer['asID'];
}
//get answers
$sql='SELECT * FROM btFormAnswers AS a WHERE a.asID IN ('.join(',',$answerSetIds).')';
$answersRS=$db->query($sql);
//load answers into a nicer multi-dimensional array
while( $answer = $answersRS->fetchRow() ){
//answer set id - question id
$answerSets[$answer['asID']]['answers'][$answer['msqID']]=$answer;
}
return $answerSets;
I would like to be able to do one of the following
A.) Move all of this into one query to be able to get the following sort of result
+---------------+------+-----------+-----------+-----------------+
| QuestionSetID | Name | Telephone | Email | Enquiry |
+---------------+------+-----------+-----------+-----------------+
| 123456 | Sean | 0800 0 | se#te.com | Asking Question |
(I did try this with various joins but could not get them quite right)
If I could get this to work I would not mind saving as a CSV
B.) Output the returned array as excel file that can be saved to a location on the server,
The current code creates a html table from the array
The code is a little long so I am only pasting the top and bottom bits here
//fwrite($handle, $excelHead);
//fwrite($handle, $row);
//fflush($handle);
ob_start();
header("Content-Type: application/vnd.ms-excel");
echo "<table>\r\n";
//Question headers go here
foreach($answerSets as $answerSetId=>$answerSet){
$questionNumber=0;
$numQuestionsToShow=2;
echo "\t<tr>\r\n";
echo "\t\t<td>". $dateHelper->getSystemDateTime($answerSet['created'])."</td>\r\n";
foreach($questions as $questionId=>$question){
$questionNumber++;
if ($question['inputType'] == 'checkboxlist'){
$options = explode('%%', $question['options']);
$subanswers = explode(',', $answerSet['answers'][$questionId]['answer']);
for ($i = 1; $i <= count($options); $i++)
{
echo "\t\t<td align='center'>\r\n";
if (in_array(trim($options[$i-1]), $subanswers)) {
// echo "\t\t\t".$options[$i-1]."\r\n";
echo "x";
} else {
echo "\t\t\t \r\n";
}
echo "\t\t</td>\r\n";
//fwrite($handle, $node);
//fflush($handle);
}
}elseif($question['inputType']=='fileupload'){
echo "\t\t<td>\r\n";
$fID=intval($answerSet['answers'][$questionId]['answer']);
$file=File::getByID($fID);
if($fID && $file){
$fileVersion=$file->getApprovedVersion();
echo "\t\t\t".''.$fileVersion->getFileName().''."\r\n";
}else{
echo "\t\t\t".t('File not found')."\r\n";
}
echo "\t\t</td>\r\n";
}else{
echo "\t\t<td>\r\n";
echo "\t\t\t".$answerSet['answers'][$questionId]['answer'].$answerSet['answers'][$questionId]['answerLong']."\r\n";
echo "\t\t</td>\r\n";
}
//fwrite($handle, $node);
//fflush($handle);
}
echo "\t</tr>\r\n";
//fwrite($handle, $row);
//fflush($handle);
}
echo "</table>\r\n";
//fwrite($handle, $excelFoot);
//fflush($handle);
//fclose($handle);
file_put_contents($filePath, ob_get_clean());
I can get the file to save to the directory but I am having issues setting it as an Excel file, I have also tried, playing with Fwrite (instead of the buffer) with the similar results
can anyone help, or point me in the right location.
Thank you,
Sean
I would do this from within concrete5. That way you get all the form-results-related models, plus the various helpers (like email).
For more info about jobs, see http://www.concrete5.org/documentation/developers/system/jobs/ . To run from a cron job, see http://www.concrete5.org/documentation/how-tos/developers/how-to-run-certain-jobs-via-cron/ .
It looks like you've got the code to generate the answers, and put it into an array, but you might want to look at something like https://github.com/concrete5/concrete5/blob/master/web/concrete/core/controllers/blocks/form_statistics.php#L32 . I'm not positive that's exactly what you need, but I do know that the dashboard page builds that answers table for you, so the code clearly exists somewhere.
Finally, to create an excel file, elsewhere c5 uses the "put it into a table and call it .xls" method, which works with excel and open office. I'm not sure exactly what you mean by "having issues setting it as Excel", but it sounds like this is your issue at the moment. If something is getting saved to the file, then you should post the file contents and you/we can work backwards as to what is causing the issue. It's probably just misformatted HTML or something.
Finally, to send the email, you can use the Mail Helper, but that doesn't currently allow for attachments (there's a pull request in github that does, and that you could use to override the mail helper with). Typically, the "best practice" would be to send it as a link.
I'm trying to rate matches based on the number of words from the query that are found in the row. I stared doing this:
SELECT Text, MATCH(`Text`) AGAINST ('$s') AS Grade
But soon I realised this didn't work since Grade is based on a lot of stuff like for example the order the words are, each word's length and so on.
I only want to know the % of words that are present in a row.
EG:
$s = 'i want pizza'
`Text` = 'pizza I want' // In this case Grade should be 100 as all words are found
Other examples:
Text | Grade
pizza I want too | 100 // All words were found, It doesn't matter if there are extra words
pizza I want | 100
i want | 66 // Only 66% of the words are present
want want want | 33 // Only 33% of the words are present
$s = 'i want pizza';
$text = 'pizza I want';
//move to lower-case to ignore case-differences
$s = strtolower($s);
$text = strtolower($text);
//count the number of words in $s
$slen = count(explode(" ", $s));
//create an array of words from the text that we check
$arr = explode(" ", $text);
$count = 0;
//go over the words from $text and count words that appear on $s
foreach ($arr as $word) {
if(strpos($s, $word) !== false){
$count++;
}
}
//display the percentage in format XX.XX
echo number_format((double)(100 * $count/$slen),2);
I have SQL query that produced more than 1000 rows. How it can get as columns ?
Example:
1
2
...
999
1000
how can I get as,
1 101 ....
2 102
... ...
99 199
100 200
following code displays
1 2 3 ...100
101 102 103 ...200
I want like this
1 101
2 102
... ...
99 199
100 200
$result=mysql_query("SELECT * FROM master_break");
$display = 10;
$cols = 0;
echo "";
while($fetched = mysql_fetch_array($result)){
if($cols == 0){
echo "\n";
}
// put what you would like to display within each cell here
echo "".$fetched['sName']." | ".$fetched['value']."";
$cols++;
if($cols == $display){
echo "";
$cols = 0;
}
}
// added the following so it would display the correct html
if($cols != $display && $cols != 0){
$neededtds = $display - $cols;
for($i=0;$i\n";
echo "";
}
echo "";
} else {
echo "";
}
?>
This will more than likely need to be done in the presentation layer (or display of data). The query can not produce multiple columns from 1 column, so you will ned to display the data as you need it in your code.
I would like to quickly display MySQL recordsets in blog posts, using a similar ASCII layout to that used by mysql.com in their help pages. I'm using wordpress v2.7. Any ideas? I should also add that I'm using wp-syntax for syntax highlighting, so it's really just generating the ASCII that I'm interesting in.
If you mean things like
+----+------+
| id | name |
+----+------+
| 1 | Bob |
| 2 | Mary |
| 3 | Jane |
| 4 | Lisa |
+----+------+
then just running the query from the MySQL commandline should suffice, as the results are formatted when running queries in interactive mode on the commandline. You can then copy and paste them into your blog post, surrounding them with <pre> or similar if necessary.
The ASCII you speak of is the way the MySQL command-line client lays out its results.
mysql> select task_nextrun,task_name from pref_task;
+--------------+-----------------+
| task_nextrun | task_name |
+--------------+-----------------+
| 1235999760 | datacache_clean |
| 1236002760 | process_stats |
+--------------+-----------------+
2 rows in set (0.00 sec)
mysql>
You would just need to send your commands to the MySQL command line client.
If you want to do it without calling the command-line client, here's a way in PHP. Note that this is pretty crude code and that I haven't tested it, I'm mostly just trying to explain the process. It will also left-align everything, I believe the mysql client right-aligns numbers, emulating that would require a bit more work, but nothing difficult.
Assuming that you've fetched the records into an associative-only array named $resultset, using something like mysqli_result's fetch_all() function:
// determine maximum value lengths for each column
foreach ($resultset as $result)
{
foreach ($result as $col => $val)
{
if (strlen($val) > $max_length[$col])
{
$max_length[$col] = strlen($val);
}
}
}
// construct border lines
foreach ($max_length as $col_length)
{
$border_line .= '+'.str_repeat('-', $col_length+2);
}
$border_line .= "+";
// print header
print $border_line."<br />\n";
foreach ($max_length as $col_name => $col_length)
{
print '| '.str_pad($col_name, $col_length, ' ').' |';
}
print "<br />\n";
print $border_line."<br />\n";
// print data
foreach ($resultset as $result)
{
foreach ($result as $col => $val)
{
print '| '.str_pad($val, $max_length[$col], ' ').' |';
}
print "<br />\n";
}
print $border_line."<br />\n";