Confused by a switch statement

Hi there.

I feel my question wouldn’t be well received on stackoverflow so came along here instead.

I am currently making this site for a client. however I have come to a roadblock so to speak.

little backstory to help better understand.

Company is a phone repair company, the client requires a page order similar to this :
device -> model -> models repair options

so here is where i am stuck.

I have got to the model page, but I don’t know what direction to go from here.

I could create a million pages for each model but thats pointless in my eyes when I am doing it all in php.

or create a switch statement that will obviously iterate through the statement and load up the page based on that particular model.

Here is what I have but i have no idea if I am doing this correctly.

[php]
if(isset($_GET[‘model’])) {
$model = $_GET[‘model’];
} else {
$model = ‘’;
}
switch ($model) {
case ‘5’:
include “iphoneModel.php”;
break;

    case '5s':
    include "iphoneModel.php";
    break; 

    .....

    case 's5':
    include "samsungModel.php";
    break;
    
    case 's6':
    include "samsungModel.php";
    break;

    .....

    default:
    include "model.php";
    break;
}

[/php]

this is the url on the other page also

model.php?model=$model'

im probably going about this all wrong, but a kick in the right direction will be much appreciated.

If any further explanations are needed hit me up and ill do my best to explain :slight_smile:

Thanks

Demigdz

Unless this is a programming exercise to use a switch statement, you wouldn’t do it this way. A switch statement would be used when you have DIFFERENT processing to do based on a value. If all you are doing is matching an input value to an output value, you would use a data driven design, where you have the values stored in a data structure (preferably a database table, but an array would work for a smaller set of data), then you would just use the input value to find and retrieve the corresponding output value. By using a data driven design, you would not need to write out program logic for each possible value, only add the input/output mapping to the data structure. The php code would be simple and would not change as the amount of data changes.

could you explain further [member=87768]phdr[/member]

sorry im fairly new to php but know enough to make a website dynamic.

If you have the time an example would be great :slight_smile: (example in no-relation to my code as would like to figure that out myself if that makes sense)

[php]<?php

// define a data structure that maps an input value (the array key/index) to an output value
$data_map = [];
$data_map[‘input_value_1’] = ‘output value 1’;
$data_map[‘input_value_2’] = ‘output value 2’;
$data_map[‘input_value_3’] = ‘output value 3’;
$data_map[‘input_value_4’] = ‘output value 4’;
// add other elements as needed

// get the input or a default value if there is no input
$model = isset($_GET[‘model’]) ? $_GET[‘model’] : ‘default input value’;

// get the output value or the default output value if there is no matching data
$output = isset($data_map[$model]) ? $data_map[$model] : ‘default output value’;

// done - $output contains the correct output value or the default output value[/php]

This only addresses the most immediate issue of not writing out block after block of logic that only differs in the values being operated on.

You would apply this same concept to your different web pages, that (should) all have the same layout, but only differ in the content being displayed. You need to have a single page layout/template that you dynamically populate with the selected content, specified by get parameter(s,)

[member=87768]phdr[/member] has you on the right track.

Where is the content coming from? Toss it into a database and use the model as a key. The database returns the differing content to a template page. Takes a handful of pages, if you don’t go the MVC route.

Thanks [member=87768]phdr[/member] will try and use this in code later.

[member=72272]astonecipher[/member] data is in db. Just not using it properly for what I want.

Sorry for brief reply just heading out

Let us know if help is needed.

Hi guys.

Appologies for a 6 day length since replying. Came down with a cold and couldn’t be bothered to do this website.

So essentially I have done the long and stupid way of doing this site.

Should I just start again with as mentioned by [member=87768]phdr[/member] with a template page and use the array to populate this template using the $_GET?

correct me if I am wrong, still getting over the cold, but running up to the deadline of this site.

Thanks for the help :slight_smile:

Download my PDO Bumpstart Database from my signature and look at the way the pages work. It is very clean and doesn’t require code gymnastics to work.

Thanks [member=46186]Kevin Rubio[/member] having a sift through now, and looking fairly simple. seem to remember this was how we did things at an old job.

Will try and have a go at structuring my site this way :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service