homepage Welcome to WebmasterWorld Guest from 174.129.80.166
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to modify this code?
scepanmali




msg:3607179
 11:11 am on Mar 21, 2008 (gmt 0)

This code works for just one Mysql table, but I want to work for all of them! I have 4 tables! If anyone know how should I do it, please help me.

<?php

// Database Connection
$username = "root";
$password = "root";
$hostname = "localhost";

$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "<br>";
$selected = mysql_select_db("spotovi1", $dbhandle) or die("Could not select DB");

$row['Artist']=$Artist;
$row['Song']=$Song;
$id= $row['id'];
$row['id']= $id;
$id = $_GET['id'];
$Artist = $_GET['Artist'];
$Song = $_GET['Song'];

$sql = mysql_query("SELECT * FROM oblast_metal WHERE ID = '$id'");

while ($row = mysql_fetch_array($sql)) { ?>

<?php $zajedno=$row['Artist']."- ".$row['Song'];

echo "<font size=3><font color=white><b>Download </b></a> </font></font>";

echo "<font size=4><font color='#A49F8B'><a href=\"{$row['Link']}\"><font color='#A49F8B'>$zajedno</font></font></a>";
echo "<font size=3><font color=white><b> Music Video</b> </a> </font></font>";
}
php?>

 

Scally_Ally




msg:3607194
 11:40 am on Mar 21, 2008 (gmt 0)

could you put your table names into an array and the loop through the array?

$tables = array("metal","rock","dance","chill");

foreach($tables as $table){

$sql = mysql_query("SELECT * FROM ".$table." WHERE ID = '$id'");
while ($row = mysql_fetch_array($sql)) { ?>
<?php
$zajedno=$row['Artist']."- ".$row['Song'];
echo "<font size=3><font color=white><b>Download </b></a> </font></font>";
echo "<font size=4><font color='#A49F8B'><a href=\"{$row['Link']}\"><font color='#A49F8B'>$zajedno</font></font></a>";
echo "<font size=3><font color=white><b> Music Video</b> </a> </font></font>";
}

}

Ally

scepanmali




msg:3607277
 2:15 pm on Mar 21, 2008 (gmt 0)

No, it just echoes 5 results from all five tables with the identical id!
Do you know some similar solution Scally_Ally? It's close, but...
Maybe with if, else if function?

eelixduppy




msg:3607552
 7:41 pm on Mar 21, 2008 (gmt 0)

>> No, it just echoes 5 results from all five tables with the identical id!

What exactly are the results you are looking for then?

scepanmali




msg:3607963
 12:39 pm on Mar 22, 2008 (gmt 0)

I just need to display the result from one selected table with id number, but it should work with all 5. Example: I have 5 separate tables. And, if I click on id in the first table, I want to display result from that table only! If I click on id in the 3th table, I want to display the result related just for that table! And I want to do that just with this code! I noticed that always display the first result correctly, but then it continues to echo the results from all 5 tables! So, I tried to limit the result with LIMIT 1 function, but it doesn't work. Like this:

$sql = mysql_query("SELECT * FROM ".$table." WHERE ID = '$id' LIMIT 1");

eelixduppy




msg:3608303
 5:15 am on Mar 23, 2008 (gmt 0)

You have to know what table you want to select from before you make your query string. You first need to remove the foreach loop, because that will give you results from all of the tables, and then you need to pass a table variable to the script somehow, either through GET or POST superglobal variables from a for on the previous page. Once you know what table you want to select from you can generate the query string accordingly, which would be very similar to what you have in your last message:

$sql = mysql_query("SELECT * FROM `".mysql_real_escape_string($_GET['table'])."` WHERE ID = '".mysql_real_escape_string($id)."'");

I wouldn't use the GET variable directly in the query without checking to make sure the table actually exists first, however, this is just an example of how it could be done accordingly to what you are looking for.

scepanmali




msg:3608417
 1:16 pm on Mar 23, 2008 (gmt 0)

I don't know how to make table variable, so I will give the code from my first page and if you can help me I will be very thankful:

$selected = mysql_select_db("spotovi1", $dbhandle) or die("Could not select DB");

if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}

// Define the number of results per page
$max_results = 20;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT * FROM oblast_metal ORDER BY id DESC LIMIT $from, $max_results");
$id= $row['id'];
$row['id']=$id;
$row['Artist']=$Artist;
$row['Song']=$Song;
while ($row = mysql_fetch_array($sql)) { ?>

<table border="0" bordercolor=#A49F8B align = "center"
cellspacing="0" cellpadding= 3 width= 500 >
<tr onMouseOver="this.bgColor='#355466';" onMouseOut="this.bgColor='#2c4453';">
<td width= "170" ><font color="white" font size=3><font face="Arial, Helvetica, sans-serif"><? echo
$row['Artist']; ?></font></td>
<td width= "200"><font color="white"><font face="Arial, Helvetica, sans-serif"><? echo
$row['Song']; ?></font></td>
<td width= "50"><font face="Arial, Helvetica, sans-serif"><a href="<?php echo "download.php?id=".$row['id'].">".$row['Artist']."- ". $row['Song'] . "";?>" target="_blank" style="text-decoration:none"><b><font color=#A49F8B>Download</b></a></a></font></td>
</tr>

<?php

echo "</table>";
}
echo "</br></br></br></br>";

echo "</center>";
echo "<center><font color=#C8C9CA><font size = 4>Select a Page<br />";
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM oblast_metal"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks

// Build Previous Link
if ($page > 1) {
$prev = ($page - 1);
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$prev\"><font color=#C8C9CA> <b><font size = 3><< Previous</a> ";
}

for ($i = 1; $i <= $total_pages; $i++) {
if (($page) == $i) {
echo "<b><font size = 3><font color = #C8C9CA>$i ";
} else {
echo "<b><font color=#C8C9CA><a href=\"" . $_SERVER['PHP_SELF'] . "?page=$i\"><style=text-decoration:none><b><font color=#C8C9CA><font size = 3>[ $i ]</a> ";
}
}

// Build Next Link
if ($page < $total_pages) {
$next = ($page + 1);
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$next\"><font color=#C8C9CA><font size = 3> Next >></a>";
}
?>

And the code from my second page is on this page!

Thank you all in advance and for helping me with this. Thank You :)

scepanmali




msg:3609309
 6:07 pm on Mar 24, 2008 (gmt 0)

Please, help me out with this. I don't know what to do.
Pleaseeeeeeeeeee! :-(

scepanmali




msg:3612684
 8:33 pm on Mar 27, 2008 (gmt 0)

If someone knows how to do this, please help me out. I really need this!
Thank You in advance!

eelixduppy




msg:3612701
 8:51 pm on Mar 27, 2008 (gmt 0)

I have given you an example of the code that you should have to work with...

You need to get a table variable from somewhere; for your case, I assume it's either going to come from a form on the page or you are going to grab it from a uri query. So for the latter case, it would look something like this before your queries:

$table = $_GET['table'];
# here is where you would do necessary checks to see if
# the table is a valid table name.

Then, you have to change your queries to have a variable table name. So, using your queries, it would be something like this:

$sql = mysql_query("SELECT * FROM `".$table."` ORDER BY id DESC LIMIT $from, $max_results");

And...:

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `".$table."`"),0);

Try making those replacements and see what you can get. You have to try to implement a little of this yourself; if you don't, you will not be learning anything for future application. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved