Dynamic table with headings and subheadings taken from database - php

I am echoing a list of areas covered as hypertext links taken from a database,
$area_shire = '';
$area_district = '';
$area_name = '';
while($rows = mysql_fetch_assoc($query)) {
if ($rows['area_shire'] != $area_shire) {
$area_shire = $rows['area_shire'];
$area_shire_url = str_replace(' ', '_', $area_shire);
echo '<h2><a href=Driveway_Cleaning_'.$area_shire_url.'>'.$area_shire.'</a></h2><br>';
}
if ($rows['area_district'] != $area_district) {
$area_district = $rows['area_district'];
$area_district_url = str_replace(' ', '_', $area_district);
echo '<h3><a href=Driveway_Cleaning_'.$area_district_url.'>'.$area_district.'</a></h3><br>';
}
if ($rows['area_name'] != $area_name) {
$area_name = $rows['area_name'];
$area_name_url = str_replace(' ', '_', $area_name);
echo '<a href=Driveway_Cleaning_'.$area_name_url.'>'.$area_name.'</a><br>';
}
}
?>
This is Giving me a linear output of
West Midlands
Birmingham
Harborne
Edgbaston
Moseley
Dudley
Halesowen
Sedgley
Warwickshire
I am trying to put the output into a dynamic table so each area_district (e.g) Dudley starts in a new column and is therefore to the right of Birmingham rather than below. Everything i try is affecting the order of the output. Any ideas?

Why don't you try to use an HTML table then? Or put each grouping in a flaoting div to where they can be placed along side each other? My main recommendation is just work out your HTML template first, then work out how to inject the dynamic data into it afterwards.

Related

Distinct values from while loop

I have a database with a field called part_name.
with the values:
Front Control Arm, Rear Control Arm
I need to echo out only Control Arm, once only.
As of now what i'm getting on the while loop results is
Control Arm, Control Arm.
Need to echo out distinct values from while loop results. I can't do it on the SQL query SELECT DISTINCT because i'm preg replacing the value from the row that i'm queering on the database.
while ($rowsparts = mysql_fetch_array($displayparts)) {
$part=''.$rowsparts['part_name'].'';
$part = preg_replace('/\bFront\b/u', '', $part);
$part = preg_replace('/\bRear\b/u', '', $part);
echo '<li>'.$part.'</li>';
}
I need to echo out the $part variable distinct values, only once per part name.
Control Arm only.
This might work if you want to only display the part name once:
$lastpart = '';
while ($rowsparts = mysql_fetch_array($displayparts)) {
$part = $rowsparts['part_name'];
$part = trim(str_replace('Rear','',str_replace('Front','',$part)));
if($lastpart != $part) {
$lastpart = $part;
echo "<li>".$part."</li>\n";
} else {
echo "<li>Part name duplicate: $part, lastpart: $lastpart</li>\n";
}
}
I added the else for debugging. It will show the two vars that are used to detect duplicates. You would remove it after testing.
This might work if you want to only display the part name once when the part names are not in order.
It builds a list of part names, and checks each part names against the list, showing
only the ones not found in the list.
$part_list = array();
while ($rowsparts = mysql_fetch_array($displayparts)) {
$part = $rowsparts['part_name'];
$part = trim(str_replace('Rear','',str_replace('Front','',$part)));
if(!isset($part_list[$part])) {
$part_list[$part] = 1;
echo "<li>".$part."</li>\n";
}
}

How to display count results with multiple keywords anywhere in a database field

We have a page tcount.php where we fetch counts for BOTH keyword1 and keyword2 occuring anywhere in a text field in mysql database using php script. The counts are displayed as hyperlinks. Now after that, we want that if someone clicks on the hyperlinks, they will see the detailed results in showt.php page showing those rows of the database corresponding to only those text field containing BOTH keywords.
The url of the second page showt.php is like
http://www.example.com/page.php?keyword=keyword1+keyword2
But problem is that it shows only those results which have keyword1 and keyword2 next to each other.
For example, keyword1 is car and keyword2 is cap and our text fields are as follows -
car cap
car best cap
car new
Then, we want it to show 1. and 2. as results but it is showing only no. 1 in the results page.
Please help.
Edit -
This is the code in page 1 tcount.php -
$kewyWordQ = $db->query("SELECT keywords FROM Table1 ");
<?php
while($row = $kewyWordQ->fetch(PDO::FETCH_ASSOC))
{$keyWord = $row['keywords'];
$keyWordsArr = explode(" ", $row['keywords']);
$countData = array();
$keyIndex = 0;
$tIndices = array();
$tArr = array();
$tIndices[] = "-1";
foreach($keyWordsArr as $keyword)
{
$t = $db->query("SELECT user_name FROM Table2 WHERE
t_text LIKE '%$keyWordsArr[$keyIndex]%'");
$tArr[] = $t;
while($row2 = $tweet->fetch(PDO::FETCH_ASSOC))
{
$found = TRUE;
foreach($keyWordsArr as $keyword1)
{
$ret = strpos(strtolower($row2['t_text']),
strtolower($keyword1));
if(($ret == 0) &&
strcmp(strtolower($row2['t_text'][0], strtolower($keyword1)[0])))
{
$found = FALSE;
break;
}
}
if($found == TRUE)
{
$ret = strpos($tIndices, $row2['t_id']);
if(($ret == 0) && strcmp($tIndices[0],
$row2['t_id']))
{
$tIndices[] = $row2['t_id'];
$countData[] = $row2['user_name'];
}
}
}
$keyIndex++;
}
?>
<tr><td><?php echo $row['keywords'];?></td>
<td><a href="showt.php?keyword=<?php echo
urlencode($keyWord); ?>" target="_blank"><?php echo count($countData); ?
></a></td>
<td><a href="showt.php?keyword=<?php echo
urlencode($keyWord); ?>" target="_blank"><?php echo
count(array_unique($countData)); ?></a></td>
</tr>
<?php } ?>
And this is the code in page 2 showt.php -
$keywords = $_GET['keyword'];
$sql="SELECT col1, col2, col3 from t AS s INNER JOIN users AS p ON
s.user_name=p.user_name where s.t_text LIKE '%$keywords%'
The WHERE statement of your query should look like:
WHERE my_text_field LIKE ?
and the binded parameter should be %$keyword1%$keyword2%
In order to define the $keyword1 and $keyword2 variables you can do something like:
list($keyword1, $keyword2) = explode(' ', $_GET['keyword']);
or you can simply use "%" . implode('%', explode(' ', $_GET['keyword'])) . "%" in case you have multiple possible keywords
There is a two way
one is hard way doing by php and mysql
$keyword = $_GET('keyword');
$keywordArray = explode(" ",$keyword);
$queryString = "";
foreach($keywordArray as $key=>$value) {
$queryString .= " column_name LIKE '%$value%' OR";
}
"SElECT * FROM table WHERE ".$queryString
second is mysql itself, that is Full text search

Group PHP ODBC results with common dataset?

I'm using PHP to connect to and display data from an ODBC MS Access database. I'm able to display the necessary data with a select statement, but I'm failing to learn how to group this data based on another dataset.
Here is my code:
<?
$handle = odbc_connect("Potter","","");
$results = odbc_exec($handle, "SELECT DISTINCT prodModelNo, prodSubType, prodCAD FROM product WHERE prodType NOT LIKE 'Plus' AND prodType = 'Fire' AND prodCAD LIKE '%CAD Drawing%' GROUP BY prodSubType");
// Results List
odbc_fetch_row($results, 0);
while(odbc_fetch_row($results)){
$cadString1 = odbc_result($results, "prodCAD");
$cadString2 = str_replace('<img src=images/i_drawing.gif> <a href=fire/', '', $cadString1);
$cadURL = str_replace(' target=_blank>CAD Drawing (zip format)</a>', '', $cadString2);
echo "<div id='file'><div id='filelink'><ul><li><a href='$cadURL'><img src='../images/download.png'/> Download</a> (zip format)</li></ul></div>";
echo odbc_result($results, "prodModelNo");
echo "<div class='clear'></div></div>";
}
?>
This outputs a list of accurate data. What I'd like to do is group results that share a common category and have that category name displayed between each group. The column for category names in the database is called prodSubType.
How would I get this to display properly?
Instead of GROUP BY prodSubType use ORDER BY prodSubType - so you can grab all items.
Then, in your code, have a BEFORE and AFTER variable to output the header differences (I'm not good at explaining this). See example (untested):
<?php
$handle = odbc_connect("Potter","","");
$results = odbc_exec($handle, "SELECT DISTINCT prodModelNo, prodSubType, prodCAD FROM product WHERE prodType NOT LIKE 'Plus' AND prodType = 'Fire' AND prodCAD LIKE '%CAD Drawing%' ORDER BY prodSubType ASC");
// Results List
odbc_fetch_row($results, 0);
$oldHeader = "";
while(odbc_fetch_row($results)){
$header = odbc_result($results, "prodSubType");
if($header != $oldHeader) {
$oldHeader = $header;
echo "<h1>$header</h1>";
}
$cadString1 = odbc_result($results, "prodCAD");
$cadString2 = str_replace('<img src=images/i_drawing.gif> <a href=fire/', '', $cadString1);
$cadURL = str_replace(' target=_blank>CAD Drawing (zip format)</a>', '', $cadString2);
echo "<div id='file'><div id='filelink'><ul><li><a href='$cadURL'><img src='../images/download.png'/> Download</a> (zip format)</li></ul></div>";
echo odbc_result($results, "prodModelNo");
echo "<div class='clear'></div></div>";
}
?>

Why does my SQL script stops?

I have a question regarding PHP/MYSQL. I have a function which gets a list of colors in a MYSQL table and renders them into numerous HTML tables. Idea is that most of the will be hidden unless the customer chooses a certain option. When the user puts his mouse over a color it loads a more detailed picture. It works fine, however after a certain number of caracters the script stops executing, here is the code:
Request:
$sql = 'SELECT *, GROUP_CONCAT(description ORDER BY i.model_correspondance ASC) AS concat, GROUP_CONCAT(model_correspondance ORDER BY i.model_correspondance ASC) AS modell, GROUP_CONCAT(element) AS elem, GROUP_CONCAT(coordinates ORDER BY i.model_correspondance ASC) AS coord FROM design_tool_items AS i WHERE i.model='.$shoe.' GROUP BY i.element, i.category, i.subelement ORDER BY i.id ASC';
And the rest of the PHP:
if ($results = Db::getInstance()->ExecuteS($sql)) {
foreach ($results as $row) {
if($row['subelement']=='') {
if(isset($i)) $code .= '</div>';
$code .= '<div class="'.$display.' color_holder" rel="'.$row['style'].'" id="div'.$row['subelement'].'.'.$row['element'].'">';
$i=1;
} else {
$code .= '</div>';
$code .= '<div class="no color_holder" rel="'.$row['style'].'" id="div'.$row['subelement'].'.'.$row['element'].'">';
}
$sentinel = $row['element'];
$code .= '<div style="text-align:left;padding:10px">'.$row['category'].'</div>';
$concat = explode(',', $row['concat']);
$coordinate = explode(',', $row['coord']);
$model_corres = explode(',', $row['modell']);
foreach($concat as $item => $corres) {
if(($i==1)&&(array_search($sentinel, $array_elements)===false)) {
$array_elements[] = $sentinel;
$select = 'selected';
} else $select = '';
$coord = explode('#', $coordinate[$item]);
$clean_array = array($row['category'], $corres);
foreach($clean_array as &$text) {
$text = str_replace(' ', '_', $text);
$text = strtolower($text);
}
$code .= '<div style="width:60px;padding-left:10px;margin:auto;float:left;">';
$code .= '<div onmouseover="Tip(\'<div><img style=width:200px;height:200px; src='.$val.'folded_images/'.$clean_array[0].'/'.$clean_array[1].'.jpg alt=Image:'.$corres.' /><br>'.$corres.'</div>\')" onmouseout="UnTip()" rel="'.$row['style'].'" denom="'.$row['subelement'].'.'.$sentinel.$model_corres[$item].'" class="color '.$select.'" style="background-position:'.$coord[0].'px '.$coord[1].'px;"></div>';
$code .= '</div>';
if(is_int($i/3)) $code .= '<div style="clear:both;"></div>';
$i++;
}
$code .= '<div style="clear:both;"></div>';
}
}
return $code;
At the 100th line the script stops working in the middle and shows only the first three characters of the description. If I reduce the length of the previous descriptions it works further so the problem comes from the array $concat.
Do you think there is a size limit (I know about the 128Mb in PHP 5.2 and 8Mb before however the website runs PHP 5.2.3 and I think I am far from 128Mb)?
Sorry about the long topic, thank you in advance to everyone who will put attention on this.
Have a good day!
Its because the use of GROUP_CONCAT.
A helpfull link that might be useful. link
Could be related to max execution time and/or memory limit. Worth tweaking each separately (and then, if in doubt, together) and see if you get favorable results. You may also want to enable better debug logging for PHP.

Making the results from a Loop, hypertext urls

I am echoing a list of areas covered from a database. The list has Headings and sub headings taken from the database,
$area_shire = '';
$area_district = '';
$area_name = '';
while($rows = mysql_fetch_array($query)):
if($rows['area_shire'] != $area_shire) {
echo '<h1>'.$rows['area_shire'].'</h1>';
$area_shire = $rows['area_shire'];
}
/* same for district using h2 and name using h3 */
endwhile;
I now want to make each result a hypertext url so i have added
$area_shire_url = str_replace(' ', '_', $area_shire);
$area_district_url = str_replace(' ', '_', $area_district);
$area_name_url = str_replace(' ', '_', $area_name);
and changed each echo to
echo '<a href=\"Tree_Surgery_'.$area_shire_url.'.php\"><h2>'.$rows['area_shire'].'<br></h2>';
$area_shire = $rows['area_shire'];}
/* same for district using h2 and name using h3 */
This has not worked at all?
I would rewrite the snipped as:
$area_shire = '';
$area_district = '';
$area_name = '';
while($rows = mysql_fetch_assoc($query)) {
if ($rows['area_shire'] != $area_shire) {
$area_shire = $rows['area_shire'];
$area_shire_url = 'Tree_Surgery_'.str_replace(' ', '_', $area_shire).'.php';
echo '<h2>'.$area_shire.'</h2><br>';
}
// same for district using h2 and name using h3
}
Your error seems to have been that you escaped the " while in a single-quoted string. When using ', php will echo all contained characters as-is; no escaping needed.
Note that I've also used mysql_fetch_assoc instead of mysql_fetch_array and rearranged the order of you HTML-tags to avoid nesting block-level elements inside inline elements.
I also choose to store the complete url in a variable, instead of just a part of it and combining it in the echo statement into the full url. This is, in my opinion, easier to read. Especially when you want to edit things later on.
'<a href=\"Tr
I don't think you need to escape the " since you're not using " to define your php statement.
You do not need to escape the double quotes as the text is not within double quotes. You also seem to have a } at the end of your code, unless this is not all of the code, this is not necessary.
I have not see this format or while loop, try changing it to this:
while($rows = mysql_fetch_array($query)){
// do stuff
}
More importantly, you have not ended your a tag. End your line with </a>

Categories