Marsha Posted September 16, 2009 Share Posted September 16, 2009 Hey, How would I collect multiple things from a database with this code: for($i=0; $i<sizeof($row['title']); $i++ ); echo '<script type="text/javascript"> function ShowPopup(hoveritem) { hp = document.getElementById("hoverpopup' . $i . '"); // Set popup to visible hp.style.visibility = "Visible"; } function HidePopup() { hp = document.getElementById("hoverpopup' . $i . '"); hp.style.visibility = "Hidden"; } </script>'; echo '<td align="center"><a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup();">',$row['title'],'</a><br /><body><div id="hoverpopup' . $i . '" style="visibility:hidden; position:absolute; z-index:1; top:300; left:20; height: 71px; width: 99px">' . $i . '</div></body>'; I was recommended the "for" code at the top but it hasn't worked :s I was told to use this: <?php for($i=0; $i<sizeof($records); $i++ ): ?> <a onMouseOver="ShowPopup('hoverpopup<?php echo $i; ?>');" onMouseOut="HidePopup('hoverpopup<?php echo $i; ?>');">link</a> <div id="hoverpopup<?php echo $i; ?>"> <!-- popup stuff --> </div> <?php endfor; ?> HELP Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 16, 2009 Share Posted September 16, 2009 Hi Marsha, Could you be a little bit more specific? The code you've supplied is basically echoing out some Javascript and a link for each row being pulled from the database. Is this what you want to ahieve? Is the above not working? Are you receiving any error messages? Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 Yeah sorry for that The link above works, but it is not Looping it, so when I hover over one Title ($row['title']) it works fine, but then I hover over a different title but still $row['title'] and it shows the same Title as the first Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 16, 2009 Share Posted September 16, 2009 What value is returned when you echo $i? In your example above you have: <div id="hoverpopup<?php echo $i; ?>"> Does that number change when you run the script? Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 You may have to double check the javascript (not my strong point), but first try this; <?php echo '<script type="text/javascript"> function ShowPopup(hoveritem) { hp = hoveritem; // Set popup to visible hp.style.visibility = "Visible"; } function HidePopup(hoveritem) { hp = hoveritem; hp.style.visibility = "Hidden"; } </script>'; for($i=0; $i<sizeof($row['title']); $i++ ) { echo '<td align="center"><a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup(this);">',$row['title'],'</a><br /><body><div id="hoverpopup' . $i . '" style="visibility:hidden; position:absolute; z-index:1; top:300; left:20; height: 71px; width: 99px">' . $i . '</div></body>'; } Also; sizeof($row['title']) can you var_dump($row['title']) to make sure you're using the correct function here. Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 What value is returned when you echo $i? In your example above you have: <div id="hoverpopup<?php echo $i; ?>"> Does that number change when you run the script? No, I changed all of the "$i"'s to $row['title'] just to check and all of the titles changed to 1, and I hovered over them, and it gave me 1 on all of them You may have to double check the javascript (not my strong point), but first try this; <?php echo '<script type="text/javascript"> function ShowPopup(hoveritem) { hp = hoveritem; // Set popup to visible hp.style.visibility = "Visible"; } function HidePopup(hoveritem) { hp = hoveritem; hp.style.visibility = "Hidden"; } </script>'; for($i=0; $i<sizeof($row['title']); $i++ ) { echo '<td align="center"><a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(this);" onMouseOut="HidePopup(this);">',$row['title'],'</a><br /><body><div id="hoverpopup' . $i . '" style="visibility:hidden; position:absolute; z-index:1; top:300; left:20; height: 71px; width: 99px">' . $i . '</div></body>'; } Also; sizeof($row['title']) can you var_dump($row['title']) to make sure you're using the correct function here. I will try that now, Thank you Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 gevans, I tried your script and I hover over the title, Nothing happens, then I move the hover and the title disappears, Bricktop I didn't really understand the question so I'm sorry If my answer was wrong lol! Quote Link to comment Share on other sites More sharing options...
Bricktop Posted September 16, 2009 Share Posted September 16, 2009 Hi Marsha, Can you post a link to the page you're using this code on? Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 Sure can http://www.rs-trainsim.co.uk/forums/ You'll need to register (No activation required) but once you have, Click downloads, and It is right there, it is the "Recent Downloads" and "Top Rated" section. The code that is active now is the one that gevans quoted apart from I changed the small bit from: <div id="hoverpopup' . $i . '" style="visibility:hidden; position:absolute; z-index:1; top:300; left:20; height: 71px; width: 99px">' . $i . '</div></body>'; } To: <div id="hoverpopup' . $i . '" style="visibility:hidden; position:absolute; z-index:1; top:300; left:20; height: 71px; width: 99px">' . $row['title'] . '</div></body>'; } Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 My bad, I didn't really pay much attention to the javascript. new code up in a second Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 here's some updated code; <?php echo '<script type="text/javascript"> function ShowPopup(hoveritem) { hp = document.getElementById(hoveritem); // Set popup to visible hp.style.visibility = "Visible"; } function HidePopup(hoveritem) { hp = document.getElementById(hoveritem); hp.style.visibility = "Hidden"; } </script>'; for($i=0; $i<sizeof($row['title']); $i++ ) { echo '<td align="center"> <a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(\'hoverpopup'. $i .'\');" onMouseOut="HidePopup(\'hoverpopup'. $i .'\');">', $row['title'], '</a> <br /> <div id="hoverpopup'. $i .'" style="visibility:hidden; position:absolute;z-index:1; top:300; left:20; height: 71px; width: 99px"> '. $i .' </div>'; } Also the content of each div seems to be a number!? Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 hehe, well I set it to numbers for the div because surely if it worked it would show 0, 1, 2, 3, 4 etc... right? but yes when It does come up like that (when it works) I will change the ' . $i . ' at the end to $row['title'] so that it pops up with the name. The code you just put up has helped, now when you hover over it, It comes up with 0, and when you stop hovering, the 0 goes and the title is still visible, so it's correct now but the Numbers are not increasing :/ Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 That's strange. Do you have more than one title? And do you have this live anywhere? Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 Yes to both, http://www.rs-trainsim.co.uk/forums/ You'll need to register (No activation required) but once you have, Click downloads, and It is right there, it is the "Recent Downloads" and "Top Rated" section. and Yes, More than one title is stored on a database, the website picks out 8 titles, 4 of which are for a top rated download, and the other 4 are the most recently added download titles. Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 Also, When you hover over the title of the "recent downloads" and "top rated", you'll see the number that pops up on the far left, 300 px down, and 20px right Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 You're not using my exact update. It's repeating the javascript which is shouldn't be in the loop... <?php echo '<script type="text/javascript"> function ShowPopup(hoveritem) { hp = document.getElementById(hoveritem); // Set popup to visible hp.style.visibility = "Visible"; } function HidePopup(hoveritem) { hp = document.getElementById(hoveritem); hp.style.visibility = "Hidden"; } </script>'; for($i=0; $i<sizeof($row['title']); $i++ ) { echo '<td align="center"> <a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(\'hoverpopup'. $i .'\');" onMouseOut="HidePopup(\'hoverpopup'. $i .'\');">', $row['title'], '</a> <br /> <div id="hoverpopup'. $i .'" style="visibility:hidden; position:absolute;z-index:1; top:300; left:20; height: 71px; width: 99px"> '. $i .' </div>'; } Look carefully at that, and if you have to copy/paste it in parts make sure the javascript is out of the loop. If you get the same problem, please paste more code. Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 I pasted that straight into it without the <?php (as it is already in a session) and it is doing the same, I don't quite understand the loop part :-\ Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 You also seem to be printing out more data within the for loop... <span class="smalltext"> Rating: (None) <br /> Downloads: 1 <br /> Views: 2 <br /> Filesize: 7.01MB <br /> Date: <b>Today</b> at 04:03:27 PM <br /> Comments (<a href="http://rs-trainsim.co.uk/forums/index.php?action=downloads;sa=view;down=124">0</a>) <br /> By: <a href="http://rs-trainsim.co.uk/forums/index.php?action=profile;u=219">The Trainspotter</a> <br /> </span> If you can show the full script it will be a lot easier Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 Hehe that would be easier for me too I clearly should stop pretending like I know what PHP and JS is :'( I have attached the PHP file that is related Thanks again for this lol! [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 ok, try replacing the whole function with this; function MainPageBlock($title, $type = 'recent') { global $db_prefix, $scripturl, $txt, $modSettings, $boardurl, $context, $user_info; if (!$context['user']['is_guest']) $groupsdata = implode($user_info['groups'],','); else $groupsdata = -1; $maxrowlevel = 4; echo '<script type="text/javascript"> function ShowPopup(hoveritem) { hp = document.getElementById(hoveritem); // Set popup to visible hp.style.visibility = "Visible"; } function HidePopup(hoveritem) { hp = document.getElementById(hoveritem); hp.style.visibility = "Hidden"; } </script>'; echo '<table cellspacing="0" cellpadding="5" border="0" align="center" width="90%" class="tborder"> <tr class="titlebg"> <td align="center" colspan="4">', $title, '</td> </tr> '; //Check what type it is $query = ' '; $query_type = 'p.ID_FILE'; switch($type) { case 'recent': $query_type = 'p.ID_FILE'; break; case 'viewed': $query_type = 'p.views'; break; case 'mostcomments': $query_type = 'p.commenttotal'; break; case 'mostdownloaded': $query_type = 'p.totaldownloads'; break; case 'toprated': $query_type = 'p.rating'; break; } $query = "SELECT p.ID_FILE, p.commenttotal, p.totalratings, p.rating, p.filesize, p.views, p.title, p.ID_MEMBER, m.realName, p.date, p.description, p.totaldownloads FROM {$db_prefix}down_file as p LEFT JOIN {$db_prefix}members AS m ON (m.ID_MEMBER = p.ID_MEMBER) LEFT JOIN {$db_prefix}down_catperm AS c ON (c.ID_GROUP IN ($groupsdata) AND c.ID_CAT = p.ID_CAT) WHERE p.approved = 1 AND (c.view IS NULL || c.view =1) GROUP by p.ID_FILE ORDER BY $query_type DESC LIMIT 4"; // Execute the SQL query $dbresult = db_query($query, __FILE__, __LINE__); $rowlevel = 0; $i = 0; while($row = mysql_fetch_assoc($dbresult)) { if ($rowlevel == 0) echo '<tr class="windowbg2">'; echo '<td align="center"> <a id="hoverover" style="cursor:default;" onMouseOver="ShowPopup(\'hoverpopup'. $i .'\');" onMouseOut="HidePopup(\'hoverpopup'. $i .'\');">', $row['title'], '</a> <br /> <div id="hoverpopup'. $i .'" style="visibility:hidden; position:absolute;z-index:1; top:300; left:20; height: 71px; width: 99px"> '. $i .' </div>'; echo '<span class="smalltext">'; if (!empty($modSettings['down_set_t_rating'])) echo $txt['downloads_form_rating'] . GetStarsByPrecent(($row['totalratings'] != 0) ? ($row['rating'] / ($row['totalratings']* 5) * 100) : 0) . '<br />'; if (!empty($modSettings['down_set_t_downloads'])) echo $txt['downloads_text_downloads'] . $row['totaldownloads'] . '<br />'; if (!empty($modSettings['down_set_t_views'])) echo $txt['downloads_text_views'] . $row['views'] . '<br />'; if (!empty($modSettings['down_set_t_filesize'])) echo $txt['downloads_text_filesize'] . format_size($row['filesize'], 2) . '<br />'; if (!empty($modSettings['down_set_t_date'])) echo $txt['downloads_text_date'] . timeformat($row['date']) . '<br />'; if (!empty($modSettings['down_set_t_comment'])) echo $txt['downloads_text_comments'] . ' (<a href="' . $scripturl . '?action=downloads;sa=view;down=' . $row['ID_FILE'] . '">' . $row['commenttotal'] . '</a>)<br />'; if (!empty($modSettings['down_set_t_username'])) { if ($row['realName'] != '') echo $txt['downloads_text_by'] . ' <a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['realName'] . '</a><br />'; else echo $txt['downloads_text_by'] . ' ' . $txt['downloads_guest'] . '<br />'; } echo '</span></td>'; if ($rowlevel < ($maxrowlevel-1)) $rowlevel++; else { echo '</tr>'; $rowlevel = 0; } $i++; } if ($rowlevel !=0) { echo '</tr>'; } echo ' </table><br />'; mysql_free_result($dbresult); } Quote Link to comment Share on other sites More sharing options...
Marsha Posted September 16, 2009 Author Share Posted September 16, 2009 Works Perfectly! I must thank you for all of your help! Thanks very much kind sir! Excellent! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.