I am trying to split the mysql query result using joomla sql syntax and stuck as to how I can split the same in three columns.
my query for which I am getting the result in single column is like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = "select m.member_name name from member m,club_name c where pst in (6,35,39,40) and c.id=m.club_name and c.id = '{club_name___id_raw}'";
if (!$query) {
echo 'Could not run query:' . mysql_error();
exit;
}
$i = 0;
$db->setQuery($query);
$results = $db->loadObjectList();
$text1 .= '<table style="border:1px solid silver;padding:2px;">';
$text1 .= "<tr><th>Past President</th><th>Past President</th></tr>";
foreach ($results as $result) {
$text .= "<tr><td>";
$text .= $result->name;
$text .= "</td></tr>";
$text .= "<br/>";
}
$text .= "</table>";
return $text1 . $text;
I am getting the result like
Past President
Jaydeven
Ashok
surendra
Narendra
Gopal
I want the result like
Past President Past President Past President
Jaydevan Ashok Surendra
Narendra Gopal
How can modify the above query to get the result as shown
Thanks.
Ok I somewhat achieved what I wanted. I have edited the query like below:
$db->setQuery($query);
$results = $db->loadObjectList();
//var_dump($results);exit;
$text1 .= '<table style="border:1px solid silver;padding:2px;width:800px;">';
$text1 .= "<tr>";
$i = 0;
foreach ($results as $result) {
if ($i % 3 === 0) {
$text .= "</tr><tr>";
}
$text .= "<td>";
$text .= $result->name;
$text .= "</td>";
$text .= $i++;
//$text .= "<br/>";
}
$text .= "</tr></table>";
return $text1 . $text;
Only problem I am now facing in my result set is I am getting a
string of number i.e. 0123456
if the result contains 7 items, which couldn't hide.
Thanks.
Related
I'm about month and a half into PHP. I tried to make function for fetching results from find_all_subjects():
function find_all_subjects() {
global $dbconnect;
$q = "SELECT * FROM subjects ORDER BY id ASC";
$subject_set = mysqli_query($dbconnect, $q);
confirm_query($subject_set);
return $subject_set;
}
and then making a new function which will loop the result in table rows. The problem is it loops just one result... when I did it with <ul> and <li> it worked fine but it doesn't seem to work when doing it with the table. My table core tags are in another document. So it's not that...
function navigacija() {
$s = find_all_subjects();
while($subjects = mysqli_fetch_assoc($s)) {
$out = "<tr>";
// Ime Teme
$out .= "<td width='25%' height='40'> <a href=\"admin_content.php?subject=" . urlencode($subjects['id']) . "\">";
$out .= $subjects['menu_name'];
$out .= "</a></td>";
// Vidljiva
if($subjects['visible'] == 1) {
$subjects['visible'] = 'DA';
} else {
$subjects['visible'] = 'NE';
}
$out .= "<td width='25%' height='40'><p align=\"center\">DA / NE";
$out .= "</p></td>";
// Broj Strana
$out .= "<td width='25%' height='40'>";
$pages_set = find_sub_from_pages($subjects['id']);
while($pages = mysqli_fetch_assoc($pages_set)) {
$out .= "" . $pages['menu_name'] . "";
$out .= "</td>";
}
// Vidljiva
$out .= "<td align=\"center\" width='25%' height='40'>";
$out .= "<img width=\"17px\" height=\"17px\" src=\"st/img/ic-arup.png\">
</img> <img width=\"17px\" height=\"17px\" src=\"st/img/ic-ardown.png\"></img> ";
$out .= "</td>";
$out .= "</tr>";
}
return $out;
}
the problem is that you reset
$out = "<tr>";
every time in the loop.
change this line to
$out .= "<tr>";
and only put declaration out of the loop
$out = "";
I have a mysql database (plattenkiste) with bandnames
id | kuenstler
1 | Aerosmith
2 | Beck
3 | Metallica
4 | Slayer
5 | Zappa, Frank
Now i want an output in alphabetical Order with directlinks and index-letters, showing only the index-letters with bandnames.
Like
Directlinks: A|B|M|S|Z
A
Aerosmith
B
Beck
M
Metallica
S
Slayer
Z
Zappa, Frank
I have a "quick and dirty" code that does what i want:
$abc = "0123456789AÄBCDEFGHIJKLMNOÖPQRSTUÜVWXYZ";
for($i=0; $i<strlen($abc); $i++){
$char = substr($abc, $i, 1);
$query = "SELECT kuenstler FROM plattenkiste WHERE kuenstler LIKE '$char%'";
$ergebnis = $mysqli->query($query);
$num = $ergebnis->num_rows;
if ($num != 0){
echo "<a href='#$char'>$char</a> | ";
}}
for($i=0; $i<strlen($abc); $i++){
$char = substr($abc, $i, 1);
$query = "SELECT kuenstler FROM plattenkiste WHERE kuenstler LIKE '$char%'";
$ergebnis = $mysqli->query($query);
$num = $ergebnis->num_rows;
if ($num != 0){
echo "<h4 id='$char'>$char</h4>";
while($row = $ergebnis->fetch_object()){
echo $row->kuenstler . '<br />';
}
}
}
Is there a shorter, more clean way to get this done?
You can have MySQL do much of the grunt for you, by both sorting the results and (for the initial list) providing only the initial characters. Then you merely need have PHP loop over the resultsets and output the necessary HTML (for the second list, adding a header whenever it encounters a new initial character).
If your plattenkiste.kuenstler column has a case sensitive collation then you may wish to explicitly specify a case insensitive collation in the ORDER BY clauses.
$query = 'SELECT DISTINCT UCASE(LEFT(kuenstler, 1)) AS char
FROM plattenkiste
ORDER BY kuenstler';
$ergebnis = $mysqli->query($query);
if ($ergebnis) {
while ($row = $ergebnis->fetch_object()) {
$char = htmlentities($row->char);
echo "<a href='#$char'>$char</a> | ";
}
}
$query = 'SELECT kuenstler FROM plattenkiste ORDER BY kuenstler';
$ergebnis = $mysqli->query($query);
if ($ergebnis) {
$row = $ergebnis->fetch_object();
while ($row) {
$c = $row->kuenstler[0];
$char = htmlentities(strtoupper($c));
echo "<h4 id='$char'>$char</h4>";
do {
echo htmlentities($row->kuenstler), '<br />';
} while ($row = $ergebnis->fetch_object() and $row->kuenstler[0] == $c);
}
}
I've had to do things like this before as well. The easiest option is to just loop through your results and build a series of arrays...
$artists = $mysqli->query(
'SELECT kuenstler FROM Lattenkiste ORDER BY kuenstler ASC'
);
$letters = array();
// get the results and sort them into arrays
while($artist = $artists->fetch_object()) {
// ge the first letter
$firstLetter = strtoupper($artist->kuenstler{0});
// if we don't have a key for it, make it
if (!isset($letters[$firstLetter])) {
$letters[$firstLetter] = array();
}
$letters[$firstLetter] = $artist->kuenstler;
}
// output the headings
echo '<ul class="directlinks">';
foreach (array_keys($letters) as $letter) {
echo '<li>' . $letter . '</li>';
}
echo '</ul>';
// now output each set of letters
foreach (array_keys($letters) as $letter) {
echo '<h4 id="' . $letter . '">' . $letter . '</h4>';
echo '<ul class="artists">';
foreach ($letters[$letter] as $kuenstler) {
echo '<li>' . $kuenstler . '</li>';
}
echo '</ul>';
}
I am using the following database query and PHP code to retrieve checkbox values and display them on the page as HTML.
$submissionId = JRequest::getInt('submissionId');
$db = JFactory::getDbo();
$db->setQuery("SELECT `FormId`, `FieldName`, `FieldValue` FROM `#__rsform_submission_values` WHERE `FormId` = '5' AND `SubmissionId` = '$submissionId' AND `FieldName` = 'documents'");
$rows = $db->loadObjectList();
foreach($rows as $row) {
$documentsArray[] = $row->FieldValue;
}
$document = implode(' ', $documentsArray);
$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
for($i = 0; $i < count($document); $i++) {
$formLayout .= "<div>" . $document . "</div>";
}
$formLayout .= '</div>';
$formLayout .= '</div>';
The code is partially working. The problem lies in the "FieldValue" row containing one or more checkbox values. I am needing to have each checkbox value formatted and displayed in it's own DIV like I have detailed below.
<div>Checkbox value #1</div>
<div>Checkbox value #2</div>
<div>Checkbox value #3</div>
I only need to show the checkbox values and not the checkboxes themselves. Any tips on how I can select each individual string from the imploded array and display it in it's own DIV. Thank you.
Just remove a little part of your code:
$submissionId = JRequest::getInt('submissionId');
$db = JFactory::getDbo();
$db->setQuery("SELECT `FormId`, `FieldName`, `FieldValue` FROM `#__rsform_submission_values` WHERE `FormId` = '5' AND `SubmissionId` = '$submissionId' AND `FieldName` = 'documents'");
$rows = $db->loadObjectList();
$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
foreach($rows as $row) {
$formLayout .= "<div>" . $row->FieldValue . "</div>";
}
$formLayout .= '</div>';
$formLayout .= '</div>';
Or implode in "divs":
...
foreach($rows as $row) {
$documentsArray[] = $row->FieldValue;
}
$document = implode('</div><div>', $documentsArray);
$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
$formLayout .= '<div>' . $document . '</div>';
$formLayout .= '</div>';
$formLayout .= '</div>';
I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )
i have script as follows
$output="<table class='products'><tr>";
while($info = mysql_fetch_array( $data )) {
//Outputs the image and other data
$output.= "<td>
<img src=http://localhost/zack/sqlphotostore/images/" .$info['photo'] ." width=323px ></img>
<b>Name:</b> ".$info['name'] . "
<b>Email:</b> ".$info['email'] . "
<b>Phone:</b> ".$info['phone'] . "</td> ";
}
$output.="<tr></table>";
print $output;
?>
it shows all results in long horizontal line how do i break the results so that they show
in new row after 3 count.
Keep a counter, and output a new table row every 3 images
$output="<table class='products'>";
$counter = 0;
while($info = mysql_fetch_array( $data ))
{
if( $counter % 3 == 0 )
$output .= '<tr>';
$output .= "<td>";
$output .= "<img src=http://localhost/zack/sqlphotostore/images/".$info['photo'] ." width=323px ></img>";
$output .= "<b>Name:</b> ".$info['name'];
$output .= "<b>Email:</b> ".$info['email'];
$output .= "<b>Phone:</b> ".$info['phone']."</td> ";
if( $counter % 3 == 0)
$output .= "</tr>";
$counter++;
}
$output.="</table>";
print $output;
?>
Add a counter and start a new row every time it reaches a multiple of 3.
$counter = 0;
while($info = mysql_fetch_array($data)) {
if ($counter++ % 3 == 0) {
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "<tr>";
}
// stuff
}
if ($counter > 0) {
$output .= "</tr>";
}
$output .= "</table>";
Please note: It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.