Help with inserting mysql data into a div

Change it to:

'SELECT `downlink`,`description`,`image`,`title`,`pid,`models` FROM `content`......

Then in phpmyadmin, I am guessing that is the tool you are using, check each of those columns and click the INDEX link below the table row list.

Added in edit:

Example: [B][COLOR=blue]http://crackfeed.com/ex1346.png[/COLOR][/B]

Mine, I used fulltext, you want INDEX… the one I circled. Check the boxes for the fields I listed above and then click the INDEX button.

Not (*), he said *

But then I said COUNT(*) which is used to count the results, not part of your code.

Okay I think i understand what you two are saying. By selecting everything im basically creating extra (unneeded) work. By selecting only the columns that are needed, I get from point a to point b quicker and with less margin for error i guess? :smiley:

It’s not margin of error… think of it this way.

You’ve got a bowl of fruit (your row).
Your friend asks you for a banana (a field in that row).
Which is going to take more effort (data transfer time/memory allocation)? Lifting the single banana (SELECT banana), or lifting the entire fruit bowl (SELECT *)?

Now, if your friend asks you for all the fruit in the bowl, then there is no difference. (And your friend is a hog, but that’s another story.)

lmao!! Okay thank you, i get it. Just a matter of saving time and effort, which i suppose adds up to alot when you have 100 friends all wanting a banana at once!

Well as for my op, I am learning!! :smiley:

mysql_select_db($dbname, $conn) or exit(mysql_error());

$loopResult = ''; // leave blank to start var for loop
$result = mysql_query('SELECT `cat_id`,`model_id`,`dl_name`,`dl_image`,`dl_image_alt`,`dl_desc`,`dl_link`,`dl_page` FROM `download` WHERE `cat_id` = "2" && (`model_id` = "1" || `model_id` = "'.$chosen.'")');
while($row = mysql_fetch_assoc($result)) {
	$loopResult = ' 
        <div class="dlitem">
         <div class="dltitle"><a href="'.$row['dl_page'].'">'.$row['dl_name'].'</a></div>
         <div class="dlimage"><a href="'.$row['dl_page'].'"><img src="'.$row['dl_image'].'" /></a></div>
         <div class="dldesc">'.$row['dl_desc'].'</div>
         <div class="dllink"><a href="'.$row['dl_link'].'" type="text/vnd.sun.j2me.app-descriptor">DOWNLOAD NOW</a></div>
        </div>
    ';
}
echo $loopResult;
?>

Thanks everyone!!

Okay Im stuck again, this is really confusing!!

Why is it when i use the ugly method, being:

$result = mysql_query('SELECT `dl_id`,`cat_id`,`model_id`,`dl_name`,`dl_image`,`dl_image_alt`,`dl_desc`,`dl_link`,`dl_page` FROM `download` WHERE `cat_id` = "2" && (`model_id` = "1" || `model_id` = "'.$chosen.'")');
while($row = mysql_fetch_assoc($result)) {
	$dl_id = $row['dl_id'];
	$dl_page = $row['dl_page'];
	$dl_name = $row['dl_name'];
	$dl_image = $row['dl_image'];
	$dl_desc = $row['dl_desc'];
	$dl_row = $row['dl_row'];
	echo" 
        <div id='.$dl_id.' class=\\"dlitem\\">
         <div id='.$dl_name.' class=\\"dltitle\\"><a href=\\"'.$dl_page.'\\">'.$dl_name.'</a></div>
         <div id='.$dl_image.' class=\\"dlimage\\"><a href=\\"'.$dl_page.'\\"><img src=\\"'.$dl_image.'\\" /></a></div>
         <div id='.$dl_desc.' class=\\"dldesc\\">'.$dl_desc.'</div>
         <div id='.$dl_link.' class=\\"dllink\\"><a href=\\"'.$dl_link.'\\" type=\\"text/vnd.sun.j2me.app-descriptor\\">DOWNLOAD NOW</a></div>
        </div>
    ";
}

It displays the 2 items in my database that match the query.

But when i do this:

$loopResult = ''; // leave blank to start var for loop
$result = mysql_query('SELECT `dl_id`,`cat_id`,`model_id`,`dl_name`,`dl_image`,`dl_image_alt`,`dl_desc`,`dl_link`,`dl_page` FROM `download` WHERE `cat_id` = "2" && (`model_id` = "1" || `model_id` = "'.$chosen.'")');
while($row = mysql_fetch_assoc($result)) {
	$dl_id = $row['dl_id'];
	$dl_page = $row['dl_page'];
	$dl_name = $row['dl_name'];
	$dl_image = $row['dl_image'];
	$dl_desc = $row['dl_desc'];
	$dl_row = $row['dl_row'];
	$loopResult = ' 
        <div id='.$dl_id.' class="dlitem">
         <div id='.$dl_name.' class="dltitle"><a href="'.$dl_page.'">'.$dl_name.'</a></div>
         <div id='.$dl_image.' class="dlimage"><a href="'.$dl_page.'"><img src="'.$dl_image.'" /></a></div>
         <div id='.$dl_desc.' class="dldesc">'.$dl_desc.'</div>
         <div id='.$dl_link.' class="dllink"><a href="'.$dl_link.'" type="text/vnd.sun.j2me.app-descriptor">DOWNLOAD NOW</a></div>
        </div>
    ';
}
echo $loopResult;

It only displays only one of the two possible matches in the query?

EDIT: I know the ugly bit of code is atrocious, but it does display both items, what is the difference here?

In the last implementation loopResult will always be equal to the value assigned in the last iteration. The = means change the value each time, essentially assigning a new value each iteration overriding the existing one. I think what your after is concatenation (notice the dot).


$loopResult = ''; // leave blank to start var for loop
$result = mysql_query('SELECT `dl_id`,`cat_id`,`model_id`,`dl_name`,`dl_image`,`dl_image_alt`,`dl_desc`,`dl_link`,`dl_page` FROM `download` WHERE `cat_id` = "2" && (`model_id` = "1" || `model_id` = "'.$chosen.'")');
while($row = mysql_fetch_assoc($result)) {
	$dl_id = $row['dl_id'];
	$dl_page = $row['dl_page'];
	$dl_name = $row['dl_name'];
	$dl_image = $row['dl_image'];
	$dl_desc = $row['dl_desc'];
	$dl_row = $row['dl_row'];
	$loopResult.= ' 
        <div id='.$dl_id.' class="dlitem">
         <div id='.$dl_name.' class="dltitle"><a href="'.$dl_page.'">'.$dl_name.'</a></div>
         <div id='.$dl_image.' class="dlimage"><a href="'.$dl_page.'"><img src="'.$dl_image.'" /></a></div>
         <div id='.$dl_desc.' class="dldesc">'.$dl_desc.'</div>
         <div id='.$dl_link.' class="dllink"><a href="'.$dl_link.'" type="text/vnd.sun.j2me.app-descriptor">DOWNLOAD NOW</a></div>
        </div>
    ';
}
echo $loopResult;

Okay thanks man, would that then mean that no unique id is needed for each div created?

EDIT: I see the uniue id is not needed with this method, thanks alot!! that is so cool!

No, no unique id is needed. However, if you want one do this:


$loopResult = '';
$i=0; // <<< create the $i variable assigned as zero
$result = mysql_query('SELECT `dl_id`,`cat_id`,`model_id`,`dl_name`,`dl_image`,`dl_image_alt`,`dl_desc`,`dl_link`,`dl_page` FROM `download` WHERE `cat_id` = "2" && (`model_id` = "1" || `model_id` = "'.$chosen.'")');
while($row = mysql_fetch_assoc($result)) {
    $i++; //Take $i and add 1 to it on each pass in the loop
    $dl_id = $row['dl_id'];
    $dl_page = $row['dl_page'];
    $dl_name = $row['dl_name'];
    $dl_image = $row['dl_image'];
    $dl_desc = $row['dl_desc'];
    $dl_row = $row['dl_row'];
    // Below, $i becomes your unique div id
    $loopResult.= ' 
        <div id="divId'.$i.'" class="dlitem">
         <div id='.$dl_name.' class="dltitle"><a href="'.$dl_page.'">'.$dl_name.'</a></div>
         <div id='.$dl_image.' class="dlimage"><a href="'.$dl_page.'"><img src="'.$dl_image.'" /></a></div>
         <div id='.$dl_desc.' class="dldesc">'.$dl_desc.'</div>
         <div id='.$dl_link.' class="dllink"><a href="'.$dl_link.'" type="text/vnd.sun.j2me.app-descriptor">DOWNLOAD NOW</a></div>
        </div>
    ';
}
echo $loopResult; 

Wow that is very cool, and im starting to see that the possibilities are truly endless!

But speaking of unique id’s, i suppose those would be useful if I wanted to track data such as the most popular downloads? But im guessing if this were the case, then the id would need to be a bit more complex because with your way “xapp” could be displayed with a div id of 1, 100 or 101 depending on the scripts output which would vary depending on the users input, or in my case $chosen?

Well this is something to certainly put some thought into, but theres no house without a foundation and speaking of that, thank you for that pic of your db freelancer, I rebuild mine as a result and spend some time looking at db normalization. I think mine would be classed as the first normal form now? In anycase, i think it’s a much better start, im going to amend your script on my test page so that it reflects the new db that, along with the few changes i made to the query and its variables.

This is exciting! :smiley:

To track downloads just have a second script redirect to the download prior to recording the traffic in your database.

Example:
http://localhost/tracking.php?id=1

the id in the url should be dl_id from your database.

so when clicked the tracking script checks to see if the dl_id exists and what the user should be redirected to. Prior to redirect it records the download like something like this:

 
mysql_query('UPDATE `download` SET `count`=(count+1) WHERE `dl_id`="' . $id . '"') or die(mysql_error());

I think mine would be classed as the first normal form now?

I didn’t quite understand what you meant here:)

Okay interesting, figured out that the problem i was haing with your script not loading was due to me inserting

$max_results = 30;

into the wrong place, i have now added it just above this line

$from = (($page * $max_results) - $max_results);"

The page now loads, but with this error:

“Fatal error: Allowed memory size of 18874368 bytes exhausted (tried to allocate 18612364 bytes) in /blah/server/root/test3.php on line 191”

Line 191 is

 ';

it appears just after this:

$loopResult.= ' 
        <div id="divId'.$i.'" class="dlitem">
         <div id='.$dl_name.' class="dltitle"><a href="'.$dl_page.'">'.$dl_name.'</a></div>
         <div id='.$dl_image.' class="dlimage"><a href="'.$dl_page.'"><img src="'.$dl_image.'" /></a></div>
         <div id='.$dl_desc.' class="dldesc">'.$dl_desc.'</div>
         <div id='.$dl_link.' class="dllink"><a href="'.$dl_link.'" type="text/vnd.sun.j2me.app-descriptor">DOWNLOAD NOW</a></div>
        </div>

What causes issues like this?

Well now, that seems like a logical approach, a million times better too! Quite interesting indeed.

But as for what i meant, umm… Well i dont exactly know myself, but i found references to that term on these pages:

MySQL :: An Introduction to Database Normalization

and

Creating A Quick MySQL Relational Database Tutorial Using All Common Relationships - Web and dedicated hosting tutorials by Anchor

was quite an interesting read.

send me the modified script. I see a double quote at:

$from = (($page * $max_results) - $max_results);"

Not sure if that is on accident when posting, but once I see your modified code I will show you the fix.

The links you posted are to to an infinite loop, not the links your posted.

LinuxFreelancer is a god amoung men :slight_smile: I cannot thank you enough for the incredible amount of insight you have brought me!!! Thank you for ALL of your help!

As for everyone else, i am truly so greatful for all the input you guys have given me. I went from knowning nothing about a bare minimum of php to oficially having my first working REAL script, which is going to help me like noone could believe :slight_smile:

The visits to my site have gone up 762.71% this month… Now common 989283829297486% HAHA!

@blackberryfan - do you really need to set up your HTML will all those divs?


<!-- your current setup -->
<div>
	<div><a href="">DL Name</a></div>
	<div><a href=""><img src="" /></div>
	<div>Description</div>
	<div><a href="">Download Now!</a></div>	
</div>

<!-- somewhat better semantic markup -->
<div>
	<h4><a href="">DL Name</a></h4>
	<a href=""><img src="" /></a>
	<p>Description</p>
	<p><a href="">Download Now!</a></p>
</div>
		

just a thought…

And thank you for the thought…

It’s one that i’m going to have to put alot of thought into before I’ll be able to reply.

But point taken, thanks!!

Edit:

$shortAnswer = $no;

There is nothing from with the divs as long as they are needed to style each line differently.