If data exists in the field print, else don't - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: If data exists in the field print, else don't (/showthread.php?tid=20203) |
If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]zebake[/eluser] Hello, new to CI and have limited php experience. I am attempting to write a student directory database application for my son's school. I have watched the tutorials and read up on the user guide. I have setup a mysql database and thanks to the video tutorials, I am able to retrieve and display the information fine. The problem I have run into is the occasional situation where a child may have two separate households (divorced parents). In this event, I need to print the information for both households. I have setup the database with the extra / alternate fields and populated them with info. How do I print the alternate contact info only if it exists, and not print anything otherwise? This is my model: Code: <?php This is my view: Code: <html> And this is my controller: Code: <?php Thanks in advance for any direction or assistance you might can provide! If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]jedd[/eluser] Hi Zebake, and welcome to the forums. You're allowed to put display-logic into your views, okay? So you can have your second lump of LI's wrapped in a conditional and just test for whether your _alt stuff is present (isset() should do it). You may want to investigate the helpers, though, as they can provide a bit nicer code for pushing out a bunch of list items. You may also want to reconsider your database schema - feel free to post it - as I suspect you are doing this inefficiently (I'm guessing that you have two address fields per person, right?). If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]zebake[/eluser] Wow, fast response, thanks Jedd! [quote author="jedd" date="1246508924"] So you can have your second lump of LI's wrapped in a conditional and just test for whether your _alt stuff is present (isset() should do it). [/quote] I think I can figure this part out, thank you. [quote author="jedd" date="1246508924"] You may want to investigate the helpers, though, as they can provide a bit nicer code for pushing out a bunch of list items. [/quote] I will look more into helpers, as well. [quote author="jedd" date="1246508924"] You may also want to reconsider your database schema - feel free to post it - as I suspect you are doing this inefficiently (I'm guessing that you have two address fields per person, right?).[/quote] You're absolutely right, I know there has to be a more efficient / logical way of doing this. Currently I have one table 'students', with 21 fields in it. Code: CREATE TABLE IF NOT EXISTS `students` ( If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]garymardell[/eluser] I'm not going to write any code currently but give you a idea of how i would do it. Have a table called student, a table called address and a table called student_address. This is because one student may require many addresses and one address may belong to many students. The student_address table allows this. The idea is to include where the primary key and foreign keys will be. Student table id | primary key name| ... Address table id | primary key name| ... student_address table student_id | composite key address_id | composite key Together act as a primary key. Now to get all the students, you will want to select everything from the students table and from the address table. Then join the student_address table where student.id = student_address.student_id And then now join address table where student_address.address_id = address.id This will return the addresses related to each student, this setup would potentially allow more than 2 addresses for a student (some complicated arrangement maybe.) or just a single address. If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]zebake[/eluser] [quote author="garymardell" date="1246512259"]The idea is to include where the primary key and foreign keys will be. Student table id | primary key name| ... Address table id | primary key name| ... student_address table student_id | composite key address_id | composite key Together act as a primary key. Now to get all the students, you will want to select everything from the students table and from the address table. Then join the student_address table where student.id = student_address.student_id And then now join address table where student_address.address_id = address.id This will return the addresses related to each student, this setup would potentially allow more than 2 addresses for a student (some complicated arrangement maybe.) or just a single address.[/quote] This sounds great, however right now it is a little bit above my level of comprehension. I'm going to eat dinner and contemplate this a bit more. Maybe it will make more sense once my belly is full. Thank you for your guidance Gary, this definitely sounds like it would offer the scalability that we'll need. If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]slowgary[/eluser] Welcome to the forums (and CI and PHP)! You've chosen your development path wisely. Jedd is right (as usual). Your database schema needs work. I'm sure it'll get the job done, but consider a situation where a student has separated parents PLUS a guardian - do you then add a THIRD alt parent to the table? You should instead have a table for students, a table for guardians, and a third table that relates the two. So: Code: students Now onto your original problem... you can simply add some logic to your view that checks if a variable is set before you attempt to echo it... Code: if(isset($row->guardian)) A few other recommendations: Try to use the most descriptive names for your functions and variables, this will help in the long run when you need to edit your code down the road (another rhyme - I'm on a roll!). So: Code: $this->Student_directory_model->get_all_students(); //CI style guide also recommends underscores and never camelCase - I agree Code: foreach($students as $student) And last but certainly not least, you should throw a few joins into your database query (since we're now working with several tables), and get your results looking like this: Code: | student_id | lname | fname | dob | guardian_fname | guardian_lname | guardian_phone | Then you could iterate through the data with something like this: Code: $temp_id = NULL; Make sense? As for the corresponding database query, I'm a little new to joins and it would require me to actually go read something. Maybe Jedd could help? ;-P (cause I know this is REAL easy for him). If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]zebake[/eluser] Man, the combination of chicken and pasta in my gut and your explanation was just what I needed! I feel so much more confident in this method, as it answers questions I didn't even think of yet. Thank you all so much for helping to make it click for me! I am going to rework the database first, and then see if I can figure out how to handle that database query. I'm not quite sure how to go about calling the information from three separate tables. If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]slowgary[/eluser] You'll need to use a few joins in your query, as garymardell stated. It'd be something along these lines (but this is probably wrong): Code: $this->db->select('students.*, guardians.fname, guardians.lname, guardians.whatever') Code: join('table', 'table.field = table2.field', 'left') If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]slowgary[/eluser] Also - don't eat and write code, it's an equation for obesity. I should know. If data exists in the field print, else don't - El Forum - 07-01-2009 [eluser]slowgary[/eluser] Hey zebake, one more thing... I mentioned in my comment that I prefer the array way of doing things, e.g.: Code: ->result_array() //I prefer this way I did a bit of testing and it turns out that the result_array() method uses quite a bit less memory. I'm sure it'd be insignificant if you were just grabbing a few rows, but in your case where you may have several thousand students * X number of guardians per student, you may be looking at a big performance gain in the memory department. So I recommend using the result_array() method, in which you would access your results like so: Code: foreach($students as $student) |