PHP - Displaying 2 Linked Tables
So I have 3 tables, student, student_course and course. I'm only using student and course atm, probably should look into student_course haha well those are the tables provided to us for the project I'm working on.
I'm trying to list all the courses from the course table and then when clicking on a course I want to display all the students who registered for this course( which would probably be the student table) Here is my code for displaying the courses: Code: [Select] <?php $display_sql ="SELECT cname FROM course ORDER BY cid DESC"; $display_query = mysql_query($display_sql) or die(mysql_error()); $rsDisplay = mysql_fetch_assoc($display_query); ?> <html> <head> </head> <body> <p> Click course to display all students registered for that course</p> <?php do { ?> <p><a href="test.php?cname=<?php echo $rsDisplay['cid']; ?>"> Course: <?php echo $rsDisplay['cname']; ?> </a> </p> <?php } while ($rsDisplay = mysql_fetch_assoc($display_query)) ?> And here is my code for displaying the students from the student table registered for the specific course you click on: Code: [Select] <?php $q = "SELECT * FROM student WHERE cname = $_GET[cname]"; $confirm_query = mysql_query($q); $rsconfirm = mysql_fetch_assoc($confirm_query); ?> <html> <head> </head> <body> <p> Displaying all student </p> <p> First Name <?php echo $rsconfirm['sname']; ?> </p> <p> Surname: <?php echo $rsconfirm['fname']; ?> </p> I'm very new to php. Im using "test.php?cname=" to transfer the cname (course name) info from my course table to the next page where I use $_GET[cname]";. cname isn't a primary key in my table, rather just a row. Can it be done like that? Or should you just use primary keys? I managed to display the courses but when clicking it, it wont display the registered users and gives me an error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\test.php on line 28 What does that mean? Line 28 is this part : Code: [Select] $q = "SELECT * FROM student WHERE cname = $_GET[cname]"; //26 $confirm_query = mysql_query($q); //27 $rsconfirm = mysql_fetch_assoc($confirm_query); //28 Any criticism welcome. When coding I'm still trying to figure out what it is I'm doing nevermind getting it to work haha. Thanks in advance Similar TutorialsHI, I have a ms-access file which has linked table. The linked table is connected to a ODBC source (e.g MS-SQL or 3rd Party ODBC Driver). WebPage(php) Apache, connecting to a ODBC DSN of MS-Access, MS-Access is having linked tables. Linked tables as from another ODBC source. On the PHP Page, When I am trying to connect the ms-access odbc dsn , I am able to connect. But when I query any of the linked tables, PHP is not able to query and is failing. Does php has capabilities to read/write to linked tables in ms-access file. Or it is issue with MS-Access Or is it I am missing something. Microsoft says, its an issue of PHP https://social.msdn....forum=accessdev Requesting you to please share your feedback Regards Rajendra Dewani I want to display a bunch of records from table 1. Table 1 has a field called "user_id". In table 2 i have id and name. Hoiw do I query table 1 and then display the name of the user? I have $query = "SELECT * from table1 where status = '$status' ORDER by ????"; $results = mysql_query($query); while $row = $mysql_fetch_array($results){ echo "status is "$row['status']." and work type is ".$row['work_type']. "for the user" $row['name']."<br>""; } Obviosuly thius wouldnt work but basically I want to pull everything fromt able 1, but sort by the name which is located in table 2. The only thing I need from table two is the name associated with user_id. Oh I ALSO want to keep all records from table1 with the same user_id on 1 line (users in table2 are unique)! Hello guys, I'm trying to figure out how to display and image that is located in one table (profile) and make it correspond to a variable on another table (latest_post) to where you echo out the variable from the table(latest_post) and the image from table(profile) appears next to the post. any how here is my code: Code: [Select] <?php $query = mysql_fetch_array(mysql_query("SELECT person FROM latest_post AND avatar_img FROM profile WHERE id FROM latest_post = id FROM profile ")); echo $query['avatar_img']; ?> Also when echoing out the query I only get the name of the image and not the image this is most likely because my image is stored in a different folder right? Thanks in advance, your help is much appreciated since I'm barely a beginner at php and MySQL. Below is a page which is supposed to output the name, blog contribution and picture of contributing members of a website. <div id="blog_content" class="" style="height:90%; width:97%; border:5px solid #c0c0c0; background-color: #FFFFFF;"> <!--opens blog content--> <?php //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //include the config file require_once("config.php"); //Define the query. Select all rows from firstname column in members table, title column in blogs table,and entry column in blogs table, sorting in ascneding order by the title entry, knowing that the id column in mebers table is the same as the id column in blogs table. $sql = "SELECT blogs.title,blogs.entry,members.firstname,images.image FROM blogs LEFT JOIN members ON blogs.member_id = members.member_id LEFT JOIN images ON blogs.member_id = images.member_id ORDER BY blogs.title ASC "; $query = mysql_query($sql); if($query !== false && mysql_num_rows($query) > 0) { while(($row = mysql_fetch_assoc($query)) !== false) { echo '<div id="blog_content1" style="float:left; position:relative;bottom:18px;left:13px; background-color: #FFFFFF; height:16.7%; width:100%; border:0px none none;" <!--opens blog_content1 same as main center top 1 and 2 from index page everything scaled down by a factor of 3, heightwise--> <div class="red_bar" style="height:3%; width:100%; border:1px solid #959595;"> <!--a--> <div class="shade1" style="height:5px; width:100%; border:0px none none;"> </div> <div class="shade2" style="height:5px; width:100%; border:0px none none"> </div> <div class="shade3" style="height:5px%; width:100%; border:0px none none"> </div> </div> <!-- closes red bar--> <div class="content" style="height:28.3%; width:100%; border:0px none none;"> <!----> <div class="slideshow" id="keylin" style="float:left; width:20%; border:0px none none;"> <!--a--> <div><img header("Content-type: image/jpeg"); name="" alt="" id="" height="105" width="105" src="$row[image]" /></div> </div> <!-- closes pic--> <div class="content_text" style="float:right; position:relative;top:7px;left:0px; max-height:150px; width:78.5%; border-width:4.5px; border-bottom-style:solid; border-right-style:solid; border-color:#c0c0c0; "> <!--a-->'; echo "<h3>".$row['title']."</h3>"; echo "<p>" .$row['entry']."<br />".$row['firstname']."</p>"; echo '</div> <!-- closes content text--> </div> <!-- closes content--> </div> <!-- closes blog_content1-->'; } } else if($query == false) { echo "<p>Query was not successful because:<strong>".mysql_error()."</strong></p>"; echo "<p>The query being run was \"".$sql."\"</p>"; } else if($query !== false && mysql_num_rows($query) == 0) { echo "<p>The query returned 0 results.</p>"; } mysql_close(); //Close the database connection. ?> </div> <!-- closes blog content--> The select query is designed to retrieve all the blog contributions(represented by the fields blogs.title and blogs.entry) from the database, alongside the contributing member (member.firstname) and the member's picture(images.image), using the member_id column to join the 3 tables involved, and outputs them on the webpage. The title, entry and firstname values are successfully displayed on the resulting page. However, I can't seem to figure out how to get the picture to be displayed. Note that the picture was successfully stored in the database and I was able to view it on a separate page using a simple select query. It is now just a question of how to get it to display on this particularly crowded page. Anyone knows how I can output the picture in the img tag? I tried placing the header("Content-type: image/jpeg"); statement at the top of the php segment, then just right below the select query and finally just right above the img tag, but in every case, I just got a big white blank page starring at me. How and where should I place the header statement? And what else am I to do to get this picture displayed? Any help is appreciated. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308347.0 Hi all, first time posting here and wondering if anyone can help me. Ive linked a button in a HTML so that it sends an email to me. But the thing thats really bugging me is that sometimes it sends the persons email address and sometimes it doesnt AND ALSO ive got multiple boxes in my HTML that I want it to send but it will only send 1 and not all 5. This is what my PHP looks like at the moment: <?php $email = $_REQUEST[ 'email_box' ] ; $message = $_REQUEST[ 'test1_box' ] ; $message = $_REQUEST[ 'test2_box' ] ; $message = $_REQUEST[ 'test3_box' ] ; $message = $_REQUEST[ 'test4_box' ] ; $message = $_REQUEST[ 'test5_box' ] ; mail( "test1049@live.com", "Test Feedback sheet", $message, "From: $email" ); ?> Can you please tell me where I have gone wrong? I really need this to be sorted out and im completely new to PHP, ive only just gotten past basic knowledge of html Hi, I want to be able to allow user to enter in <input type="text" >... the total XML files to upload on the first page so: my first page has form that asks user how many uploads form they want so (firstpage.html): <form method='post' action='outputTotalUploads.php' >... Then in second form (outputTotalUploads.php) is where it outputs the desired number of upload forms based on the first page: for loop to post each <form method='post' action='addToDb.php' >... Finally in third page (addToDb.php): for each uploaded file, do: //BUT HOW DO I GET THIS TOTAL?? add to database *My problem is HOW do I refer to the total number of uploads the user chose way back in the first page b/c I know you cannot have multiple action attribute in the form tag. Any help much appreciated! ...in a different folder? For instance, I have a stylesheet in my C:\xampp\htdocs\rcm folder, and I want to link it to files in the C:\xampp\htdocs\ folder. Is there a way of doing this? Thanks in advance Simplified for everyone's sanity and re-posted.
Got a Wordpress Conference theme, lets you enter speakers into a database and assign each one a category/role. In the settings for the theme, there's a dropdown from which you can select one of the roles you create. Doing so will display all the speakers assigned that role in the footer of the homepage and also populate a stand-alone Speakers page with the same role. I want to make it so that the footer on the homepage displays one role and the stand-alone speaker page displays another. Here's the PHP for the Speakers page (identical to the footer PHP) <?php Here's the HTML for the "dropdown" portion of the settings, where you can see the role categories available. "Speaker" is the default created by the Theme, "whyattend" is the one I made. <!-- Speakers Category --> I want "whyattend" to show on the Footer and "Speaker" to populate the Speakers page. Can this be done? I have no idea where to start or which board would be the proper place to put this but what I"m attempting to do is integrate this idea with my custom cms I'm making now is that once inside I can take down my site in case I (administrator) wanted to perform some maintenance on my site and have it instead display a custom 404 page. Any ideas on where to start. I tried using my best friend google but he gave me nothing. Maybe a few tutorials of this might help. Hi, I have been told it is possible to use PHP to design a left navigation bar which features a submenu for each link clicked on. So if you click "shoes" in the left navigation bar it will then display all the left navigation bar links but below shoes it might have a sub menu saying "Size 5", "Size 6", "Size 7", "Size 8", "Size 9", "Size 10". What function in PHP allows me to include submenus? I think I need to define a 'Foreign Key' and use categories but I do not know what function to use to alter the menu depending on what the user clicks/selects??! Any ideas? Matt. This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=347820.0 This portion is kind of stumping me. Basically, I have a two tables in this DB: users and users_access_level (Separated for DB normalization) users: id / username / password / realname / access_level users_access_level: access_level / access_name What I'm trying to do, is echo the data onto an HTML table that displays users.username in one table data and then uses the users.access_level to find users_access_level.access_name and echo into the following table data, I would prefer not to use multiple queries if possible or nested queries. Example row for users: 1234 / tmac / password / tmac / 99 Example row for users_access_level: 99 / Admin Using the examples above, I would want the output to appear as such: Username: Access Name: Tmac Admin I am not 100% sure where to start with this, but I pick up quickly, I just need a nudge in the right direction. The code I attempted to create just shows my lack of knowledge of joining tables, but I'll post it if you want to see that I did at least make an effort to code this myself. Thanks for reading! Im doing a search system and Im having some problems.
I need to search in two tables (news and pages), I already had sucess doing my search system for just one table, but for two tables its not easy to do.
I already use a select statment with two tables using UNION because I want to show number of search results, that is number of returned rows of my first sql statment.
But now I need to do a select statment that allows me to acess all fields of my news table and all fields of my pages table.
I need to acess in my news table this fields: id, title, content, link, date, nViews
I need to acess in my pages table this fields: id, title, content, link
Im trying to do this also with UNION, but in this case Im not having any row returning.
Do you see what I have wrong in my code?
<?php //first I get my $search keyword $search = $url[1]; $pdo = connecting(); //then I want to show number of returned rows for keyword searched $readALL = $pdo->prepare("SELECT title,content FROM news WHERE title LIKE ? OR content LIKE ? UNION SELECT title,content FROM pages WHERE title LIKE ? OR content like ?"); $readALL->bindValue(1,"%$search%", PDO::PARAM_STR); $readALL->bindValue(2,"%$search%", PDO::PARAM_STR); $readALL->bindValue(3,"%$search%", PDO::PARAM_STR); $readALL->bindValue(4,"%$search%", PDO::PARAM_STR); $readALL->execute(); //I show number of returned rows echo '<p>Your search keyword returned <strong>'.$readALL->rowCount().'</strong> results!</p>'; //If dont return any rows I show a error message if($readALL->rowCount() <=0){ echo 'Sorry but we didnt found any result for your keyword search.'; } else{ //If return rows I want to show, if it is a page result I want to show title and link that I have in my page table //if it is a news result I want to show title and link that I have in my news table and also date of news echo '<ul class="searchlist">'; $readALL2 = $pdo->prepare("SELECT * FROM news WHERE status = ? AND title LIKE ? OR content LIKE ? LIMIT 0,4 UNION SELECT * FROM pages where title LIKE ? OR content LIKE ? LIMIT 0,4"); $readALL2->bindValue(1, '1'); $readALL2->bindValue(2, "%$search%", PDO::PARAM_STR); $readALL2->bindValue(3, "%$search%", PDO::PARAM_STR); $readALL2->bindValue(4, "%$search%", PDO::PARAM_STR); $readALL2->execute(); while ($result = $readALL2->fetch(PDO::FETCH_ASSOC)){ echo '<li>'; echo '<img src="'.BASE.'/uploads/news/'.$result['thumb'].'"/>'; echo '<a href="'.BASE.'/news/'.$result['id_news'].'">'.$result['title'].'</a>'; //if it is a news result I also want to show date on my list //echo '<span id="date">'.$result['date'].'</span>'; echo '</li>'; } echo ' </ul>'; //but how can I do my select statement to have access to my news table fields and my page table fields?? } ?> I am trying to write some data from multiple SQL tables to a page. In the first table is a list of places. I then have more tables that are named after the different places. For example, say my first place in the list is called Place1. I have a table named Place1 with data that corresponds to place1. The data contained in the table named Place1 is a list of things to do in this place. It has 21 columns and each one is something to do in the morning, afternoon, and at night for each day of the week in the place Place1. What I am trying to do is display a sort of weekly calendar as a table on a webpage that lists all of the places in one column and then lists seven days of the week as 7 more columns. Then in each data cell I would want to list the things to do in the morning, afternoon and at night for the certain day of the week and for the place. The problem is that I am creating a CMS to allow other users with no coding knowledge to update events for other places, so I have to display data that could have been altered. The only solution I know of is to do a while loop that gets all of the place names and while there are still place names, write the place names to the page and set a variable equal to the place name. Inside the while loop I would create another while loop that each time the first while loop is executed uses the variable set in the first while loop to know which table to reference and then make a call to that table pulling out the 21 columns and writing them to the page. Each time the outer while loop executes, it would (hopefully) write the place name, and then set the variable as the current place name so that the inner while loop uses the variable to write the rest of the information about that place. I don't know if that would even work and if it did, I know it cannot be the best way to do this. I am pretty stuck here and don't really have a good solution so if anyone proposes a solution that is radically different to anything I have done, I am open to everything. Thank you! Hi All, first of all. Sorry for signing up to the site and then immediately asking for help. I hate doing it but always seems to be the way people get involved with forums these days and it seems I am no different... So thank you in advance if you are kind enough to answer! Among many personal flaws, my issue is a technical one regarding the ability to retain session variables. Unfortunately whilst the website itself is not complicated, the problem is not as simple as just running session_start(); and letting it get on and do its own thing. So the problem exists around a relatively simple contact form and the basic Turing test that it uses to prove the user is human. The script adds a value to the $_SESSION variable and then on the next page load, tests to see if the variable is still there. As per this little function: function cookiesEnabled() { if ($_SESSION['cookiesEnabledTest']) $_SESSION['cookiesEnabled'] = true; else $_SESSION['cookiesEnabledTest'] = true; } Now for what must be 95% of all users, the variable is still there, proving the the session is being maintained as the user navigates the site. Unfortunately a small group of computers appear to be having difficulties with this for a not too clear reason. The computers have varying browser user agents indicating that they are all (so far) using one of the following: MSIE 8 MSIE 7 MSIE 7 in what appears to be MSIE 6 compatibility mode Now in testing, I can't get MSIE 8 or MSIE 7 to produce the same effect, with exception to MSIE 8 in "InPrivate" browsing mode. In this mode it is easy to see that there is no cookie txt file being created and thus no way of the browser to pass back the session id on the next page load. I can't think of any detail that I have missed out, but if there is anything I have left unclear, please let me know! Thanks again! Felix Okay, so I am trying to display players usernames and kills and deaths from my highscores page and I have different div classes set for each block This is my code:
<div id="table"> <div class="tr ttl"> <div class="col st1">Rank</div> <div class="col st2">Username</div> <div class="col st3">Rating</div> <div class="col st4">Deaths</div> <div class="col st5">Kills</div> </div> <?php if($_GET['skill'] == 0) { $skill = $_GET['skill']; $page = ($_GET['page']-1); $limit = RESULTS_PER_PAGE*$page.', '.RESULTS_PER_PAGE; $limitt = RESULTS_PER_PAGE*$page; $query = "SELECT * FROM hs ORDER BY `elo` DESC LIMIT ".$limit.""; $result = mysql_query($query); $counter = 1; while ($fetch = mysql_fetch_assoc($result)) { echo '<div class="tr normal ftr rd1"> <div class="col st1">'.($counter+$limitt).'</div> <div class="col st2"><a href="?user='.$fetch['username'].'">'.$fetch['username'].'</a></div> <div class="col st3">'.number_format($fetch['kills']).'</div> <div class="col st4">'.number_format($fetch['deaths']).'</div> <div class="col st5">'.$fetch['elo'].'</div> </div> '; $counter++; } } ?>See where it says echo '<div class="tr normal ftr rd1"> I'd want it to print out like this: (tr normal ftr rd1) for the first one, (tr normal ftr rd2) for second one and (tr normal ftr rd3) for the 3rd one. Then (tr normal) and (tr odd normal) for the rest Can anyone help me out here? Quick background... I bought an auction program with plenty of bugs. Todays bug that I can't figure out is that it is displaying a - (dash) when I would prefer it to display 0 (zero) in instances of "total fees due" or in the "fee chart" if it's a flat fee of 0. 0% works fine. The code related to it is spread out on a handful of files but I think it's mostly related to the following. if (is_array($setup_fee)) { foreach ($setup_fee as $key => $value) { if ($value['amount']) { if ($value['calc_type'] == 'flat') { $output['amount'] += $value['amount']; $fee_display = $this->display_amount($value['amount'], $this->setts['currency']); } else if ($value['calc_type'] == 'percent') { $output['amount'] += $this->round_number($item_details['start_price'] * $value['amount'] / 100); $fee_display = $value['amount'] . '%'; } if ($item_relist) { $output['amount'] = $this->round_number($output['amount'] - $output['amount'] * $this->fee['relist_fee_reduction'] / 100); } $output['display'] .= '<tr class="c1"> '. ' <td width="150" align="right"><b>' . GMSG_SETUP_FEE . ':</b></td> '. ' <td align="left" nowrap colspan="2"><b>' . $fee_display . $value['display'] . '</b></td> '. '</tr> '; ## now add the row on the invoices table if ($user_payment_mode == 2 && $add_invoices) { $account_balance += $output['amount']; $tax_settings = $this->tax_amount($output['amount'], $this->setts['currency'], $user_details['user_id'], $this->setts['enable_tax']); $sql_insert_invoice = $this->query("INSERT INTO " . DB_PREFIX . "invoices (user_id, item_id, name, amount, invoice_date, current_balance, can_rollback, tax_amount, tax_rate, tax_calculated, reverse_id) VALUES ('" . $user_details['user_id'] . "', '" . $item_details['auction_id'] . "', '" . GMSG_SETUP_FEE . "', '" . $output['amount'] . "', '" . CURRENT_TIME . "', '" . $account_balance . "', " . $can_rollback . ", '" . $tax_settings['amount'] . "', '" . $tax_settings['tax_rate'] . "', '1', '" . $item_details['reverse_id'] . "')"); $sql_update_user_balance = $this->query("UPDATE " . DB_PREFIX . "users SET balance='" . $account_balance . "' WHERE user_id='" . $user_details['user_id'] . "'"); } } } } foreach ($fees_no_tier as $key => $value) { if ($value[1]) { $fee_details = $this->display_fee($key, $user_details, $item_details['category_id'], $item_details['list_in'], $voucher_details, $apply_tax); if ($item_relist) { $fee_details['amount'] = $this->round_number($fee_details['amount'] - $fee_details['amount'] * $this->fee['relist_fee_reduction'] / 100); } $output['amount'] += $fee_details['amount']; if ($fee_details['amount']) ## only do this if there is a fee { $output['display'] .= '<tr class="c1"> '. ' <td width="150" align="right"><b>' . $value[0] . ':</b></td> '. ' <td nowrap colspan="2"><b>' . $fee_details['display'] . '</b></td> '. '</tr> '; ## now add the row on the invoices table if ($user_payment_mode == 2 && $add_invoices) { $account_balance += $fee_details['amount']; $tax_settings = $this->tax_amount($fee_details['amount'], $this->setts['currency'], $user_details['user_id'], $this->setts['enable_tax']); $sql_insert_invoice = $this->query("INSERT INTO " . DB_PREFIX . "invoices (user_id, item_id, name, amount, invoice_date, current_balance, can_rollback, tax_amount, tax_rate, tax_calculated, reverse_id) VALUES ('" . $user_details['user_id'] . "', '" . $item_details['auction_id'] . "', '" . $value[0] . "', '" . $fee_details['amount'] . "', '" . CURRENT_TIME . "', '" . $account_balance . "', " . $can_rollback . ", '" . $tax_settings['amount'] . "', '" . $tax_settings['tax_rate'] . "', '1', '" . $item_details['reverse_id'] . "')"); $sql_update_user_balance = $this->query("UPDATE " . DB_PREFIX . "users SET balance='" . $account_balance . "' WHERE user_id='" . $user_details['user_id'] . "'"); } } } } $output['display'] .= '<tr class="c5"><td colspan="3"></td></tr><tr class="c2"> '. ' <td width="150" align="right"><b>' . GMSG_TOTAL . ':</b></td> '. ' <td nowrap colspan="2"><b>' . $this->display_amount($output['amount'], $this->setts['currency']) . '</b></td> '. '</tr> '. '<tr class="c5"> '. ' <td><img src="themes/' . $this->setts['default_theme'] . '/img/pixel.gif" width="150" height="1"></td> '. ' <td colspan="2"><img src="themes/' . $this->setts['default_theme'] . '/img/pixel.gif" width="1" height="1"></td> '. '</tr> '; return $output; } I'll really really appreciate the help. I'm trying to grab something on a different page with the layout: Code: [Select] 0 day, 10 hours, 35 min, 59 sec My code to display this information on another page is: <?php $page_contents = file_get_contents("http://www.mysite.com"); $matches = array(); preg_match('/([0-9,]+) day, ([0-9,]+) hours, ([0-9,]+) min, ([0-9,]+) sec /', $page_contents, $matches); echo $matches[0]; ?> Why is it displaying nothing? |