looksgoodhoss looksgoodhoss - 3 months ago 8
MySQL Question

Why is "Array" in my string?

The code I am working on allows the user to enter a search query, which will display related/similar items from the database. These related/similar items will be wrapped in anchor tags and div tags.

The following code: get the query, create an array, execute function, add bullet points to each value excluding the last one, and return.

$Query = $_SESSION["Query"];
$GLOBALS["sgtSource"] = array();

searchChannels($Query);

$lastElement = end($GLOBALS["sgtSource"]);
foreach($GLOBALS["sgtSource"] as $key => $value)
{
if($value != $lastElement)
{
$sgtSource .= $value . " • ";
}
else
{
$sgtSource .= $value;
}
}

$html .= "<div class='sgtPanel Font' style='color: #4c4c4c'>" . $sgtSource . "</div>";
echo $html;


The function code: search database for related/similar items, generate a link from the item, add to associative array (so it's unique), return.

function searchChannels($Query)
{
global $conn;
if($Query != "")
{
// Searching channels
$sql = "SELECT * FROM ChannelName WHERE ChannelName LIKE '%" . $Query . "%'";
$myData = mysqli_query($conn, $sql);

while($record = mysqli_fetch_array($myData))
{
$ChannelID = $record[0];
$ChannelName = $record[1];
if($ChannelID != null)
{
$sgtLink = "<a href='../web/channel.php?CID=$ChannelID#Cat=Highlights' class='noLink'><span class='Font'>" . $ChannelName . "</span></a>";
$GLOBALS["sgtSource"][$ChannelID] = $sgtLink;
}
}
}
return;
}


I cannot search for multiple queries - I know this. I need to rewrite my code to include an IN function in my SQL. This isn't my problem.

My problem is what gets displayed. Regardless of what I search, the beginning will always contain the word "Array". For example, if I search "United" and var_dump() the result, I will get the following:
string(113) "Array United Kingdom". The count is high because of the anchor tags, etc. But what I cannot understand is why "Array" is there.

I do not understand where "Array" is coming from - I do not have much experience with associative arrays (see my last question), so forgive me if the solution is blindingly obvious.

EDIT 1:

if(!is_array($value))
{
$sgtSource .= "I am not an array";
}
if(!is_array($key))
{
$sgtSource .= "I am not an array";
}


EDIT 2:
Thanks everyone for the help. My problem was that I never knew that $GLOBALS["sgtSource"] was the same as $sgtSource. Therefore, as Manish and Shoyeb suggested, I renamed the original $sgtSource to $sgtSource1. Thus having $GLOBALS["sgtSource"] and $sgtSource1.

Answer
$Query = $_SESSION["Query"];

$GLOBALS["sgtSource"] = array();

$sgtSource1=''; $html='';

searchChannels($Query);

$lastElement = end($GLOBALS["sgtSource"]);

foreach($GLOBALS["sgtSource"] as $key => $value)

{

if($value != $lastElement)
{
    $sgtSource1 .= $value . " &#8226; ";
}
else
{
    $sgtSource1 .= $value;
}
}

 $html .= "<div class='sgtPanel Font' style='color: #4c4c4c'>" .$sgtSource1 . "</div>";
echo $html;

$sgtSource is already a global array you declared, it cannot be used as a string, so I made a new variable $sgtSource1 that is a string.