Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing variables 2

Status
Not open for further replies.

captlid

Technical User
Oct 27, 2004
82
US
ok this is probably very simple but I cant figure out how to do it. :(

$query2 = "select * from links, linkscat where linked = 1 and category ='".$_GET['cat']."' and category = linkscat.id";

$result2 = mysql_query($query2);

echo $catname;

while ($row2 = mysql_fetch_assoc($result2)) { ?>
<?=$row2['description']?></div><?
$catname = $row2['name']; }
?>

I am getting catname undefined variable error.
Due to the visual layout the variable must be above the while statement.... Is there anyway to initialize it beforehand or do something to get it to display...
 
Hi there.
Can you please describe what you want to achieve visually?
Like, is that a list of category with description,
or list of the links to this particular category, but you want to have the category name of the main category first?

Please confirm.
 
I took out all the html code, cause its a php question, I am not using tables for this, just one div, and line breaks
thanks for the quick response...
 
I still don't get you.
I know what the problem is, but to help you to resolve to what you wanted, then I need to know the visual display.

If you want to debug only, I could only tell you that your catname is not defined, and your register_globals is turned off.
Therefore, you will get error.

If you want to get fixed and displayed accordingly, you might as well let me know so I could help you out. :)
 
Your original code:
Code:
$query2 = "select * from links, linkscat where linked = 1 and category ='".$_GET['cat']."' and category = linkscat.id";

$result2 = mysql_query($query2);

echo $catname;

while ($row2 = mysql_fetch_assoc($result2))  { ?>
<?=$row2['description']?></div><?
$catname = $row2['name']; }
?>
You said you need '$catname' to display first. Using your code, it can't be done. Try this version:
Code:
$query2 = "select * from links, linkscat where linked = 1 and category ='".$_GET['cat']."' and category = linkscat.id";

$result2 = mysql_query($query2);
$tmp1 = array();
$tmp2 = array();
while ($row2 = mysql_fetch_assoc($result2))  {
   $tmp1[] = $row2['name'];
   $tmp2[] = $row2['description'];
   // </div>
   // I'm not sure where this fits into your scheme since I don't see an opening <div> tag
}

echo implode("<br>\n",$tmp1)."<br>\n";
echo implode("<br>\n",$tmp2)."<br>\n";
?>
I [red]think[/red] will give you what you want...

Ken
 
Code:
<? 
$query2 = "select url, website, description, name from links, linkscat where linked =1 and category ='".$_GET['cat']."' and category = linkscat.id";

$result2 = mysql_query($query2); echo $query2;

$catname = array();
echo $catname;

?>
<div>
<p>Links >><?=$catname?></p>
<?
while ($row2 = mysql_fetch_assoc($result2)) { ?>
<div class="linkbox">
<a href="[URL unfurl="true"]http://<?=$row2[/URL]['url']?>" id="linkbox"><?=$row2['website']?></a><?=$row2['description']?>
</div><?

$catname[] = $row2['name'];

}
?>
</div><br><br>

maybe this will help understand more....
 
Ok. Get you.
Try this.

<?
$query2 = "select url, website, description, name from links, linkscat where linked =1 and category ='".$_GET['cat']."' and category = linkscat.id";

$result2 = mysql_query($query2); echo $query2;

$catname = array();
$desc = array();
while ($row2 = mysql_fetch_assoc($result2)) {
$desc[] = "<div class=\"linkbox\">
<a href=\"['url']."\" id=\"linkbox\">".$row2['website']."</a>".$row2['description']."</div>\r\n";
$catname[] = $row2['name'];
}
foreach ($catname as $category)
{
echo $category."<br>";
}
?>
<p>Links >><?=$catname?></p>
<?
foreach ($desc as $description)
{
echo $description;
}
?>
 
That's a little better...

When you echo a variable that is an array, it does not print the values of the array, all that it prints is the word "array".

Try this:
Code:
<?
$query2 = "select url, website, description, name from links, linkscat where linked =1 and category ='".$_GET['cat']."' and category = linkscat.id";

$result2 = mysql_query($query2); echo $query2;

$catname = array();
$tmp = array();
while ($row2 = mysql_fetch_assoc($result2)) {
   $tmp[] = '<div class="linkbox">';
   $tmp[] = '<a href="[URL unfurl="true"]http://'[/URL] . $row2['url'] . '" id="linkbox">' . $row2['website'] . '</a>' . $row2['description'];
   $tmp[] = '</div>';
   $catname[] = $row2['name'];
}
echo '<div>'."\n";
echo '<p>Links >>'. implode("<br>\n",$catname).'</p>';
echo implode("<br>\n",$tmp)."\n";
echo '</div><br><br>'."\n";
?>

Ken
 
ok both your options worked, but theres one little problem..

The output becomes
links >> category

depending on the amount of records the query gets, thats how many words get outputted. For example if theres three records the screen shows

links >> categorycategorycategory

thank you so much for your help so far.
 
//foreach ($catname as $category)
//{
//echo $category."<br>";
//}
?>
<p>Links >> <?=$catname[0]?></p>

ok I got rid of the the above lines and added the [0];

looks like its working

:)

thanks for everything
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top