Jump to content

Recommended Posts

Hi, C++ is my language, but since I know some html, and I was asked to do a website for a friend, I thought I might as well take this chance to learn some PHP as well. Well I already created an application that takes the user's input and inserts it into a table called tblVendors located in a database called turbosha_Vendors. That script works fine, but now I am trying to make a script that grabs the the data from the table that has an ID field, which is set to contain an integer value and, as the primary key, it auto-increments as new rows are added, and displays it on a a webpage.

 

Here is the PHP code I have. Keep in mind that the database username and password were replaced with X's for security purposes.

 

<?PHP
$Database = mysql_connect("localhost", "XXXX", "XXXX") or die(mysql_error());
mysql_select_db("turbosha_Vendors") or die(mysql_error()); ;
$result  = mysql_query("SELECT MAX(ID) as maxID FROM tblVendors")or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$max = $row[maxID];
$result2 = mysql_query("SELECT MIN(ID) as minID FROM tblVendors")or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$min = $row2[minID];

echo "<br>";
echo "<br>";


echo "<center>";
echo "<table border=\"0\" width=\"100%\">";
echo "<th style=\"background-color:#cc0000;font-family:Verdana;color:#e8e8e8;font-size:20px\">";
echo "Vendor List";
echo "</th><tr><td><b style=\"font-family:Verdana;color:#cc0000;font-size:20px\">";

for($ID = $min;$ID != $max;$ID++)
{
  $field = mysql_query("SELECT * FROM tblVendors where ID=$ID",$Database) or die(mysql_error());
  $cname = mysql_result($field,1,"CompanyName");
  $URL = mysql_result($field,2,"URL");
  $rname = mysql_result($field,3,"RepName");
  $remail = mysql_result($field,4,"RepEmail");

echo("<a href=\"$URL\">$cname</a>");

echo "<br>";
echo "<i style=\"font-family:Verdana;color:#cc0000;font-size:10px\">";
echo "-Representative's Name: ";
echo "$rname";
echo "</i>";
echo "<br>";
echo "<i style=\"font-family:Verdana;color:#cc0000;font-size:10px\">";
echo "-Representative's Email:";
echo "$remail";
echo "</i>";
echo "<br><br></center>";
}
?>

 

 

Lastly, here is the link to the page so you can see what is going wrong.

 

http://www.turboshack.com/vendors/list.php

 

Thanks in advance,

Jon

Link to comment
https://forums.phpfreaks.com/topic/156702-new-to-php-what-am-i-doing-wrong-d/
Share on other sites

I am new to PHP as well, but maybe this could help you. I wrote this code to retrieve data from a MySQL database. There are a few differences from your code. I use a WHILE loop instead of a FOR statement like you did. I would also recommend using CSS to change the display of the page, instead changing it in the HTML tags itself.

 

Here is the code I used to display my data. I saved this file as F_NcompDate.php

 

<?php
function createOptions(){
$host="localhost";
$user="";
$password="";

$cxn = mysql_connect($host,$user,$password) or die ("Couldn't connect to database.");

$dbname = 'ABCDEFG';
mysql_select_db($dbname);

$query = "SELECT * FROM `workorder` WHERE `Completed`='0' ORDER BY `Date_Filed` DESC, `Location` ASC";
$result = mysql_query($query)
	or die (mysql_error());

while($row = mysql_fetch_array($result)) // Loop thru recordset and get values
{	
	 echo "<tr><td width=\"40\">"; echo $row["Date_Filed"]; echo "</td>";
 echo "<td width=\"50\">"; echo $row["WorkOrder_ID"]; echo "</td>";
 echo "<td width=\"70\" align=\"center\">"; echo $row["Location"]; echo "</td>";
 echo "<td width=\"80\">"; echo $row["By"]; echo "</td>";
	 echo "<td width=\"260\">"; echo $row["Problem"]; echo "</td>";
 echo "<td width=\"260\">"; echo $row["Comments"]; echo "</td>";
 echo "<td width=\"40\">"; echo $row["Date_Completed"]; echo "</td></tr>";
}
} // End createOptions()
?>

 

I had to call this function on another php page. Here is the code I used to call the this function.

 

<?php include("F_NcompDate.php"); //include your function(s) ?>
<head>
<title>Title of Page</title>
<link href="css/gobal.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header1">
<img src="images/hi.gif" alt="Holiday Inn Logo" width="200" height="83" id="logo"/>
        <p align="right"><a href="home.html" target="_self" class="headlink">Home</a> | <a href="reports.html" target="_self" class="headlink">Reports</a> </p>
        <h1 align="center">Header</h1>
</div>
<div id="reportContainer">
<h1 align="center">Non Completed Work Orders By Finished Date</h1>
<table border="1" width="800">
<tr> 
        <th>Dated Filed</th>
	<th>Work Order #</th>
        <th>Location</th>
        <th>Filed By</th>
        <th>Problem</th>
        <th>Solutions</th>
        <th>Target Date</th>

   </tr>
        <?php createOptions(); //execute your function defined in 'F_NcompDate.php' ?>
</table>
</div>
</body>
</html>

 

Hope this helps.

Well, I did what Ken2k7 told me and now it works a bit better. It actually displays the first item in the database, however, it is not lopping around to display the other ones... Also, instead of displaying the data in the fields when they are called, it displays the ID number...

 

perrij3, I tried your code and it does the same thing mine is doing right now. No loop... Yours does, indeed, display the right stuff, however, instead of just the ID.

 

Here is the current code:

<?PHP
function data_get()
{
$Database = mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error()); ;
$result  = mysql_query("SELECT MAX(ID) as maxID FROM table_here")or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$max = $row["maxID"];
$result2 = mysql_query("SELECT MIN(ID) as minID FROM table_here")or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$min = $row2["minID"];


for($ID = $min;$ID != $max;$ID++)
{
  $field = mysql_query("SELECT * FROM tblVendors where ID=$ID",$Database) or die(mysql_error());
  $cname = mysql_result($field,"CompanyName");
  $URL = mysql_result($field,"URL");
  $rname = mysql_result($field,"RepName");
  $remail = mysql_result($field,"RepEmail");

echo("<a href=\"$URL\">$cname</a>");

echo "<br>";
echo "<i style=\"font-family:Verdana;color:#cc0000;font-size:10px\">";
echo "-Representative's Name: ";
echo "$rname";
echo "</i>";
echo "<br>";
echo "<i style=\"font-family:Verdana;color:#cc0000;font-size:10px\">";
echo "-Representative's Email:";
echo "$remail";
echo "</i>";
}
}
?>

 

and here is my calling of the function:

 

...
<table border="0" width="100%">
<th style="background-color:#cc0000;font-family:Verdana;color:#e8e8e8;font-size:20px">
Vendor List
</th>
<tr>
<td>
<?php data_get(); ?>
</td>
</tr>
</table>
...

 

an lastly, again, here is the URL so that you cna see what is happening:

http://www/turboshack.com/vendors/list.php

What if you change your SQL statement to something like this:

 

$query = "SELECT * FROM `table_here` ORDER BY `ID` DESC"

 

This would get all the data from this table and put it descending order. If you want it in ascending order, use ASC instead of DESC. This would eliminate needing to find the max and min ID numbers. I would then try using the while loop again.

That is just *HORRIBLE* code. I know you're a newbie blah blah.... Sorry for that... actually no, I'm really not. :D

 

Learn from this. :) Anything you don't understand, please ask us.

 

<?php
function data_get() {
$Database = mysql_connect('localhost', '', '') or die(mysql_error());
mysql_select_db('') or die(mysql_error()); ;
$result  = mysql_query('SELECT * FROM tblVendors') or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
  $cname = $row['CompanyName'];
  $URL = $row['URL'];
  $rname = $row['RepName'];
  $remail = $row['RepEmail'];

echo '<a href="$URL">' . $cname . '</a>';
echo '<br />';
echo '<i style="font-family:Verdana;color:#cc0000;font-size:10px;">-Representative\'s Name: ' . $rname . '</i>';
echo '<br />';
echo '<i style="font-family:Verdana;color:#cc0000;font-size:10px;">-Representative\'s Email: ' . $remail . '</i>';
}
}
?>

 

There, that should be better.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.