Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
site search help needed
Hi.
I need some basic help with the angorithm of db search please. No code, just suggestions on the algorithm. thnks
I have two related tables in the mysql db, and i need to create a search. The parent table stores brands. The child table stores products, and one its fields is
Problem: to create a search that would seach both products and brands, so that if i query nokia 6630 i have this product in the results. Currently, I have it seach only products table; it queries product title (e.g. 6630) fine, but when i add the brand to it in the seach field, it fetches nothing. Logically, the seach is right, couse there is no product title Nokia. Thus, I need to make it search brands too somehow. And I need your help with the algorithm of this search.
Currentlly, it is the following:
But for this new search i need, I suppose it should be somrthing like,
.. or something... Am i right in this? Help please
Here is the snippet (don't mind the encoding problem):
I need some basic help with the angorithm of db search please. No code, just suggestions on the algorithm. thnks
I have two related tables in the mysql db, and i need to create a search. The parent table stores brands. The child table stores products, and one its fields is
brand_id
. I think you get the idea here. Problem: to create a search that would seach both products and brands, so that if i query nokia 6630 i have this product in the results. Currently, I have it seach only products table; it queries product title (e.g. 6630) fine, but when i add the brand to it in the seach field, it fetches nothing. Logically, the seach is right, couse there is no product title Nokia. Thus, I need to make it search brands too somehow. And I need your help with the algorithm of this search.
Currentlly, it is the following:
if matches found in products table, count them and then for each of them output values
else say "no results"
But for this new search i need, I suppose it should be somrthing like,
if matches found in products table, count them and then for each of them output values
else search for brands table
if mathes found there count and output
else say no results
.. or something... Am i right in this? Help please
Here is the snippet (don't mind the encoding problem):
$q1=db('SELECT * FROM new_products WHERE title LIKE \'%'. $q .'%\' ORDER BY title');
if (rows($q1)>0) {
$all=rows($q1);
echo '<p style="margin-left: 10px">Ãî çà ïðîñó «<var>' .$q.'</var>»
Ãà éäåÃ(î) ' .$all. ' ðåçóëüòà ò(a/îâ).</p><ol>';
while ($s=fasoc($q1)) {
$b=fassoc('SELECT * FROM brands WHERE id='.$s['brand']);
echo '...'; // output goes here
}
}
0
This discussion has been closed.
Comments
encoding failed
the algorithm is rather primitive, it goes like:
if match found in products then count and output
else
if match is found in brands count and output
else
if there is a space in the query then explode to q1 and q2 and search brands for q1
if matches found count them and for each search products for q2 if mathes found then count and output
$q1=db('SELECT * FROM new_products WHERE title LIKE \'%'. $q .'%\' ORDER BY title'); if (mysql_num_rows($q1)>0) { $all=mysql_num_rows($q1); echo '<p style="margin-left: 10px">Ãî çà ïðîñó «<var>' .$q.'</var>» Ãà éäåÃ(î) ' .$all. ' ðåçóëüòà ò(a/îâ) <small>(ïîèñê ïî ìîäåëÿì)</small>.</p><ol>'; while ($s=fasoc($q1)) { $b=fassoc('SELECT * FROM brands WHERE id='.$s['brand']); echo '<li><img style="margin: 0 11px 0 7px; float: right" src="' .$s['img1']. '" height="50px" alt="' .$b['title']. ' ' .$s['title']. '"><b><a href="/index.php?page=catalogue&pcat=' .$b['category']. '&brand=' .$b['title']. '&model=' .$s['title']. '">' .$b['title']. ' ' .$s['title']. '</a></b><br /> <p>' .(empty($s['synopsis'])? (empty($s['description_small']) ? (substr($s['description'], 1, 100)) : $s['description_small']) : $s['synopsis']). '</p><br style="clear: both">'; } echo '</ol>'; } else { $q2=db('SELECT * FROM brands WHERE title LIKE \'%'. $q .'%\' ORDER BY title'); if (mysql_num_rows($q2)>0) { $all=mysql_num_rows($q2); echo '<p style="margin-left: 10px">Ãî çà ïðîñó «<var>' .$q.'</var>» Ãà éäåÃ(î) ' .$all. ' ðåçóëüòà ò(a/îâ) <small>(ïîèñê ïî áðåÃäó)</small>.</p><ol>'; while ($s=fasoc($q2)) { echo '<li><img style="margin: 0 11px 0 7px; float: right" src="' .$s['logo']. '" height="50px" alt="' .$s['title']. '"><b><a href="/index.php?page=catalogue&pcat=' .$s['category']. '&brand=' .$s['title']. '">' .$s['title']. '</a></b><br style="clear: right"/></li>'; } echo '</ol>'; } else { if (ereg(' ', $q)) {//echo 'there is a space'; $r=explode(' ', $q); $q3=db('SELECT * FROM brands WHERE title LIKE \'%'. $r[0] .'%\' ORDER BY title'); if (mysql_num_rows($q3)>0) { //$all=mysql_num_rows($q3); //echo '<p style="margin-left: 10px">Ãî çà ïðîñó «<var>' .$q.'</var>» // Ãà éäåÃ(î) ' .$all. ' ðåçóëüòà ò(a/îâ).</p><ol>'; while ($s=fasoc($q3)) { $q4=db('SELECT * FROM new_products WHERE title LIKE \'%'. $r[1] .'%\' ORDER BY title'); if (mysql_num_rows($q4)>0) { $all+=mysql_num_rows($q4); echo '<p style="margin-left: 10px">Ãî çà ïðîñó «<var>' .$q.'</var>» Ãà éäåÃ(î) ' .$all. ' ðåçóëüòà ò(a/îâ) <small>(ñìåøà ÃÃûé ïîèñê)</small>.</p><ol>'; while ($sp=fasoc($q4)) { echo '<li><img style="margin: 0 11px 0 7px; float: right" src="' .$sp['img1']. '" height="50px" alt="' .$s['title']. ' ' .$sp['title']. '"><b><a href="/index.php?page=catalogue&pcat=' .$s['category']. '&brand=' .$s['title']. '&model=' .$sp['title']. '">' .$s['title']. ' ' .$sp['title']. '</a></b><br /> <p>' .(empty($sp['synopsis'])? (empty($sp['description_small']) ? (substr($sp['description'], 1, 100)) : $sp['description_small']) : $sp['synopsis']). '</p><br style="clear: both">'; } echo '</ol>'; } else { echo $empty; } //echo '<li><img style="margin: 0 11px 0 7px; float: right" src="' .$s['logo']. '" height="50px" alt="' .$s['title']. '"><b><a href="/index.php?page=catalogue&pcat=' .$s['category']. '&brand=' .$s['title']. '">' .$s['title']. '</a></b><br />'; } echo '</ol>'; } else { echo $empty; } } else { echo $empty; } } }