Please Anyone Solve Itneel_basu wrote:Now I Cant Decide Ho To Arrange The $curr_arr According To $num_arr
To Sort The Products
No This Code Will Not Get Slower That Much
Moderator: General Moderators
Code: Select all
$hld_key_stat .= $curr_key."(". $num_match .")"." ";Code: Select all
$hld_key_stat .= $curr_key."(". $num_match .")".",";But The $curr_arr Doesn't Get Arrenger In Descending Orderneel_basu wrote:Now I Cant Decide How To Arrange The $curr_arr According To $num_arr
To Sort The Products
It Will Be Easier To Handelisaac_cm wrote:fetch me 3 results of the higher 3 products
I really did understand what you mean by this sentenceSo If You Remofe The First 3 From Your Conditions Thats It Will Show All The Matching Records of The Keyword Of The Current Product
Code: Select all
<?php
//As You Are Separating Keywords By Space The Individual Jeywords Must Not Contain Spaces
$keyw = $_GET['product'];//You Can Also Use POST/Cookies Etc..
$host = "localhost";
$usr = "root";
$psw = "";
$db_name = "key"
$tbl_name = "shop"
$key_field = "key"
$product_field = "product";
$conn = mysql_connect($host, $usr, $psw) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$sql = "select `$key_field` from `$tbl_name` where `$product_field` = '$keyw'";
$res = mysql_query($sql, $conn) or die(mysql_error());
//Assuming There Is No Repetation Of Product Name In the $product_field Coloumn of The Table
//So that is mysql_num_rows = 1
$curr_key = explode(" ", $res);
for($i=0;$i<=count($curr_key);$i++)
{
$sql_loop = "select `$key_field` from `$tbl_name` where `$product_field` like '$curr_key[$i]'";
$res_loop = mysql_query($sql_loop, $conn) or die(mysql_error());
$num_match = mysql_num_rows($res_loop);
$prod[$i][0] = $curr_key;
$prod[$i][1] = $num_match;
}
//Here $prod[$i][0] holds The Keyword And $prod[$i][1] Holds The Number Of Occurences
//**********************************************************************//
//**************It Is Not Arranged In Descending Order******************//
//**********************************************************************//
foreach($prod as $c)
{
while(list($k,$v) = each($c))
{
echo "$k .... $v";
}
}
?>Look At The Code Carefullyisaac_cm wrote:no this way I get only the first 3 products, it could be done using limit 3
I will explain what I need in steps:
1- get the current selected products keywords and store it in a variable or array
2- loop through all the keywords, in each loop I have to get the number of finding this keyword in all products of the current category (ex: bags)
(ex: keywords = "satin bag(5), classy(9), magnetic clasp(2), Dark(7), Navy(4), Fabric(15)"
3- compare all number and get the higher 3 results (Fabric(15), classy(9), Dark(7)) then get one product id from each of the 3 results to display as a recommended product and exclude the current one of course
thanks
Code: Select all
<?php
function neelmax( $array )
{
foreach( $array as $value )
{
if( is_array($value) )
{
$subvalue = neelmax($value);
if( $subvalue > $return )
{
$return = $subvalue;
}
}
elseif($value > $return)
{
$return = $value;
}
}
return $return;
}
?>
<?php
//As You Are Separating Keywords By Space The Individual Jeywords Must Not Contain Spaces
$keyw = $_GET['product'];//You Can Also Use POST/Cookies Etc..
$host = "localhost";
$usr = "root";
$psw = "";
$db_name = "key"
$tbl_name = "shop"
$key_field = "key"
$product_field = "product";
$conn = mysql_connect($host, $usr, $psw) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$sql = "select `$key_field` from `$tbl_name` where `$product_field` = '$keyw'";
$res = mysql_query($sql, $conn) or die(mysql_error());
//Assuming There Is No Repetation Of Product Name In the $product_field Coloumn of The Table
//So that is mysql_num_rows = 1
$curr_key = explode(" ", $res);
for($i=0;$i<=count($curr_key);$i++)
{
$sql_loop = "select `$key_field` from `$tbl_name` where `$product_field` like '$curr_key[$i]'";
$res_loop = mysql_query($sql_loop, $conn) or die(mysql_error());
$num_match = mysql_num_rows($res_loop);
$prod[$i][0] = $curr_key;
$prod[$i][1] = $num_match;
}
//Here $prod[$i][0] holds The Keyword And $prod[$i][1] Holds The Number Of Occurences
//**************************************************************************************//
//****************************It Is Not Arranged In Descending Order********************//
//**************************************************************************************//
$max = neelmax($prod);
for($i=0;$i<=count($prod);$i++)
{
if($prod[$i][1] == $max)
{
$top_prod_id = $i;
}
}
$sql_prod = "select `$product_field` from `$tbl_name` where `$key_field`` = '$prod[$top_prod_id][0]' && `$product_field` != '$keyw'";
$res_prod = mysql_query($sql_prod,$conn) or die(mysql_error());
while($db_arr = mysql_fetch_array($res_prod))
{
$top_prod_name = $db_arr[$product_field];
echo $top_prod_name;
}
if(mysql_num_rows($res_prod) == 0)
{
for($i=0;$i<=count($prod);$i++)
{
$prob_sql .= "`$key_field`` = '$prod[$i][0]' || ";
}
$prob_sql_final = substr($prob_sql,0,strlen($prob_sql)-3);
$org_sql = "select `$product_field` from `$tbl_name` where ".$prob_sql_final."&& `$product_field` != '$keyw'";
//echo $org_sql;//This Line Is To Test IfThe SQL Command Is Wrong To Test It Just Remove The Comment Before echo
$res_prod_oth = mysql_query($org_sql,$conn) or die(mysql_error());
while($db_arr_oth = mysql_fetch_array($res_prod_oth))
{
$prod_name_oth = $db_arr_oth[$product_field];
echo $prod_name_oth;
}
}
?>Code: Select all
<?php
function neelmax( $array )
{
foreach( $array as $value )
{
if( is_array($value) )
{
$subvalue = neelmax($value);
if( $subvalue > $return )
{
$return = $subvalue;
}
}
elseif($value > $return)
{
$return = $value;
}
}
return $return;
}
?>
<?php
//As You Are Separating Keywords By Space The Individual Jeywords Must Not Contain Spaces
$keyw = $_GET['product'];//You Can Also Use POST/Cookies Etc..
$host = "localhost";
$usr = "root";
$psw = "";
$db_name = "key"
$tbl_name = "shop"
$key_field = "key"
$product_field = "product";
$product_id = "prod_id";
$conn = mysql_connect($host, $usr, $psw) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
$sql = "select `$key_field` from `$tbl_name` where `$product_field` = '$keyw'";
$res = mysql_query($sql, $conn) or die(mysql_error());
//Assuming There Is No Repetation Of Product Name In the $product_field Coloumn of The Table
//So that is mysql_num_rows = 1
$curr_key = explode(" ", $res);
for($i=0;$i<=count($curr_key);$i++)
{
$sql_loop = "select `$key_field`,`$product_id` from `$tbl_name` where `$product_field` like '$curr_key[$i]'";
$res_loop = mysql_query($sql_loop, $conn) or die(mysql_error());
$num_match = mysql_num_rows($res_loop);
$prod[$i][0] = $curr_key;
$prod[$i][1] = $num_match;
while($prod_id_wh = mysql_fetch_array($res_loop))
{
$prod[$i][2] = $prod_id_wh[$product_id];
}
}
//Here $prod[$i][0] holds The Keyword And $prod[$i][1] Holds The Number Of Occurences
//**************************************************************************************//
//****************************It Is Not Arranged In Descending Order********************//
//**************************************************************************************//
$max = neelmax($prod);
for($i=0;$i<=count($prod);$i++)
{
if($prod[$i][1] == $max)
{
$top_prod_id = $i;
}
}
$sql_prod = "select `$product_field`,`$product_id` from `$tbl_name` where `$key_field`` = '$prod[$top_prod_id][0]' && `$product_field` != '$keyw'";
$res_prod = mysql_query($sql_prod,$conn) or die(mysql_error());
while($db_arr = mysql_fetch_array($res_prod))
{
$top_prod_name = $db_arr[$product_field];
$top_prod_id = $db_arr[$product_id];
echo $top_prod_name;
}
if(mysql_num_rows($res_prod) == 0)
{
for($i=0;$i<=count($prod);$i++)
{
$prob_sql .= "`$key_field`` = '$prod[$i][0]' || ";
}
$prob_sql_final = substr($prob_sql,0,strlen($prob_sql)-3);
$org_sql = "select `$product_field`,`$product_id` from `$tbl_name` where ".$prob_sql_final."&& `$product_field` != '$keyw'";
//echo $org_sql;//This Line Is To Test IfThe SQL Command Is Wrong To Test It Just Remove The Comment Before echo
$res_prod_oth = mysql_query($org_sql,$conn) or die(mysql_error());
while($db_arr_oth = mysql_fetch_array($res_prod_oth))
{
$prod_name_oth = $db_arr_oth[$product_field];
$prod_id_oth = $db_arr_oth[$product_field];
echo $prod_name_oth."...."$prod_id_oth;
}
}
?>