Page 1 of 1

duplicate data been displayed

Posted: Wed Jan 21, 2009 3:12 pm
by bruisedghost
Hi all

I am having a problem where I created a funtion that echos in html, the funtion is called upon multiple times in the html and as a result is displaying the same data. I wanted to know if there is a way around this to display only unique data. Here are the snippets from my code


here is where I have setup the $locate funtion.

Code: Select all

$querydiamond = "SELECT Vendor_Code,Product_Code,Product_Name,Product_Description,Product_Picture,Product_Price,Product_Link FROM merchant WHERE Dmnd = 'x'";
 
$result = mysql_query($querydiamond) or die ("Error in query: $query. ".mysql_error());
 
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
 
    echo "<table cellpadding=10 border=1>";
    while($row = mysql_fetch_row($result)) {
$locate= $row[4];
 

and here is the html where I echo the funtion.

Code: Select all

<div class="TabbedPanelsContentGroup">
        <div class="TabbedPanelsContent">
          <table width="100%" height="100%" border="0" cellpadding="5" align="center">
            <tr>
              <td><a href="11211011-0124.html"><img src="product_thumbs/<?php echo $locate; ?>" alt="" width="140" height="140" border="0" /></a></td>
 
 
 
              <td><a href="11211011-0128.html"><img src="product_thumbs/<?php echo $row[4]; ?>" alt="" width="140" height="140" border="0"/></a></td>
              <td><a href="11211011-0130.html"><img src="product_thumbs/11211011-0130.jpg" alt="" width="140" height="140" border="0"/></a></td>

I do realizr that I am echoing the same funtion twice and that is probably where I am getting my problem, but is there a way I can do this so that the $locate funtion only pulls back unique sets of data?


Thanks!

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 3:21 pm
by Burrito
I don't see any functions anywhere.

I see you're setting a local variable to an item from your row array and yes, you're echoing the same value by echoing the local variable and the item in the array.

What are you looking to achieve here?

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 3:38 pm
by bruisedghost
sorry about the confussion as I am quite new to php,

I need the local variable to echo unique data from the array to be more specific, I need that variable to echo the next item in the row and not repeat it.

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 3:43 pm
by Burrito
for starters, use mysql_fetch_assoc() to create an associative array. It's much easier to work with associative arrays when dealing with data from a mysql server than numeric indices.

I"m not sure what you mean by you need the local variable to echo unique data from the array...do you mean a different column from the table row? If so, then do as I suggested and use the associative array and just echo it like this:

Code: Select all

 
echo $row['column1'];
echo $row['column2'];
 

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:04 pm
by bruisedghost
thanks Ive switched everything over to an associative array, however what Im trying to do is list all of the values in the row so that when I echo that $locate variable multiple times different values appear. I have tried to <?php echo row[mycolumnname]; ?> within the html however it does not appear to be returning any values.

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:10 pm
by Burrito
You'll need to reference the value using the column name (from the table) as the array key.

Code: Select all

 
while($row=mysql_fetch_assoc($result))
{
  //this value will change for each iteration of the loop provided that it's different on each database row...yourcolumn is just an example, you need to replace that with whatever the column name is in your table.
  $locate = $row['yourcolumn'];
}
 

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:23 pm
by bruisedghost
I already have it setup like that for the current column the problem is that I need to use the $locate variable 5 times the way the html table is setup and each time I use it it pulls back the same data.
the $locate variable essentially contains the url of a image file in the root directory. because of this when I reference the $locate variable the same image is showing up five times.

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:27 pm
by Burrito
are you resetting the variable within your while loop?

I just looked at your OP and the fourth index is Product_Picture.

so you should be using:

Code: Select all

 
$locate = $row['Product_Picture'];
 
within your while loop.

It's really not necessary to create the local variable however.

Code: Select all

 
echo "<img src=\"pathtoimages/".$row['Product_Picture']."\">";
 

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:42 pm
by bruisedghost
hmm its still not returning any data

Code: Select all

<div class="TabbedPanelsContentGroup">
        <div class="TabbedPanelsContent">
          <table width="100%" height="100%" border="0" cellpadding="5" align="center">
            <tr>
              <td><a href="11211011-0124.html"><?php echo "<img src=\"product_thumbs/".$row['Product_Picture']."\">"; ?> alt="" width="140" height="140" border="0" /></a></td>
...
 
 

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:44 pm
by Burrito
is that within your while loop?

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:47 pm
by bruisedghost
the variable $locate is but not that bit of code

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:49 pm
by Burrito
you need to include whatever you want 'looped' inside the loop.

Re: duplicate data been displayed

Posted: Wed Jan 21, 2009 4:52 pm
by bruisedghost
thanks dude! new I was doing something stupid