Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Czystość kodu, Jak zmienić ten straszliwy kod!!
Wertas
post
Post #1





Grupa: Zarejestrowani
Postów: 45
Pomógł: 0
Dołączył: 2.03.2010

Ostrzeżenie: (0%)
-----


Witam. Z góry chcę podkreślić, że jestem słaby w pisaniu dobrego i czystego kodu ponieważ, powtarzam go na okrągło. Temu chciałbym aby ktoś pokazał mi jak można to zrobić, dużo przy tym się nauczę wejdzie mi to w nawyk.

Teraz przejdźmy do rzeczy.

Oto SQL bazy danych

  1. CREATE TABLE `items` (
  2. `id` int(10) NOT NULL AUTO_INCREMENT,
  3. `Strength` int(11) NOT NULL,
  4. `Dexterity` int(11) NOT NULL,
  5. `Intelligence` int(11) NOT NULL,
  6. `Vitality` int(11) NOT NULL,
  7. `Lok` int(11) NOT NULL,
  8. `Lph` int(11) NOT NULL,
  9. `Lphg` int(11) NOT NULL,
  10. `Lifesteal` int(11) NOT NULL,
  11. `Additional Life` int(11) NOT NULL,
  12. `Lps` int(11) NOT NULL,
  13. `IArcane` int(11) NOT NULL,
  14. `ICold` int(11) NOT NULL,
  15. `IFire` int(11) NOT NULL,
  16. `IHoly` int(11) NOT NULL,
  17. `ILightning` int(11) NOT NULL,
  18. `IPoison` int(11) NOT NULL,
  19. `IWeapon` int(11) NOT NULL,
  20. `AttackSpeed` int(11) NOT NULL,
  21. `Increased Minimum Damage` int(11) NOT NULL,
  22. `Increased Maximum Damage` int(11) NOT NULL,
  23. `CHD` int(11) NOT NULL,
  24. `Wd` int(11) NOT NULL,
  25. `Chc` int(11) NOT NULL,
  26. `Bdmgvsel` int(11) NOT NULL,
  27. `CBlind` int(11) NOT NULL,
  28. `CChill` int(11) NOT NULL,
  29. `CFear` int(11) NOT NULL,
  30. ...... itd
  31. UNIQUE KEY `id` (`id`),
  32. KEY `id_2` (`id`)
  33. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Ogólnie skrypt jest przeznaczony do wpisywania wartości w te pola by ludzie mogli zobaczyć co możemy zaoferować.

Tutaj jest przykład mojej bezmyślności i niedołęstwa

1)Fragment kodu


  1. if($_REQUEST['do'] == 'list')
  2. {
  3. echo '<table border="1"><thead>
  4. <th>id</th>
  5. <th>Strength</th>
  6. <th>Dexterity</th>
  7. <th>Intelligence</th>
  8. <th>Vitality</th>
  9. <th>Lok</th>
  10. <th>Lph</th>
  11. <th>Lphg</th>
  12. <th>Lifesteal</th>
  13. <th>Additional Life</th>
  14. <th>Lps</th>
  15. <th>IArcane</th>
  16. <th>ICold</th>
  17. <th>IFire</th>
  18. <th>IHoly</th>
  19. <th>ILightning</th>
  20. <th>IPoison</th>
  21. <th>IWeapon</th>
  22. <th>AttackSpeed</th>
  23. <th>Increased Minimum Damage</th>
  24. <th>Increased Maximum Damage</th>
  25. <th>CHD</th>
  26. <th>Wd</th>
  27. <th>Chc</th>
  28. <th>Bdmgvsel</th>
  29. <th>CBlind</th>
  30. <th>CChill</th>
  31. <th>CFear</th>
  32. <th>CFreeze</th>
  33. <th>CImmobilize</th>
  34. <th>CKnockback</th>
  35. <th>CSlow</th>
  36. <th>CStun</th>
  37. <th>Ctbleed</th>
  38. <th>Bleeddmg</th>
  39. <th>Gf</th>
  40. <th>Mf</th>
  41. <th>Exp</th>
  42. <th>Indestructible</th>
  43. <th>Movement Speed</th>
  44. <th>Yardspick</th>
  45. <th>Reduced Level Requirement</th>
  46. <th>Sock1</th>
  47. <th>Sock2</th>
  48. <th>Sock3</th>
  49. <th>Resistance All</th>
  50. <th>RArcane</th>
  51. <th>RCold</th>
  52. <th>RFire</th>
  53. <th>RHoly</th>
  54. <th>RLightning</th>
  55. <th>RPhysical</th>
  56. <th>RPoison</th>
  57. <th>Increased Armor</th>
  58. <th>CCred</th>
  59. <th>Meleeatd</th>
  60. <th>Reduced Damagefeli</th>
  61. <th>Chance to blockomshield</th>
  62. <th>Chance to blockomattr</th>
  63. <th>Block Amount</th>
  64. <th>Type</th>
  65. <th>Price</th>
  66. <th>Seller</th>
  67. </thead>';
  68. WHILE ($row = mysql_fetch_array($result)){
  69. echo '
  70. <tr><td>'.$row['id'].'</td>
  71. <td>'.$row['Strength'].'</td>
  72. <td>'.$row['Dexterity'].'</td>
  73. <td>'.$row['Intelligence'].'</td>
  74. <td>'.$row['Vitality'].'</td>
  75. <td>'.$row['Lok'].'</td>
  76. <td>'.$row['Lph'].'</td>
  77. <td>'.$row['Lphg'].'</td>
  78. <td>'.$row['Lifesteal'].'</td>
  79. <td>'.$row['Additional Life'].'</td>
  80. <td>'.$row['Lps'].'</td>
  81. <td>'.$row['IArcane'].'</td>
  82. <td>'.$row['ICold'].'</td>
  83. <td>'.$row['IFire'].'</td>
  84. <td>'.$row['IHoly'].'</td>
  85. <td>'.$row['ILightning'].'</td>
  86. <td>'.$row['IPoison'].'</td>
  87. <td>'.$row['IWeapon'].'</td>
  88. <td>'.$row['AttackSpeed'].'</td>
  89. <td>'.$row['Increased Minimum Damage'].'</td>
  90. <td>'.$row['Increased Maximum Damage'].'</td>
  91. <td>'.$row['CHD'].'</td>
  92. <td>'.$row['Wd'].'</td>
  93. <td>'.$row['Chc'].'</td>
  94. <td>'.$row['Bdmgvsel'].'</td>
  95. <td>'.$row['CBlind'].'</td>
  96. <td>'.$row['CChill'].'</td>
  97. <td>'.$row['CFear'].'</td>
  98. .... itd do każdego ze schematu SQL
  99. </tr>';
  100. }
  101. echo '</table>';
  102. }


Jak to w ogóle wygląda? Czy macie jakiś pomysł jak bardzo to skrócić?

Nie wiem czy jest szansa stworzenia jakiegoś PDO czy czegoś tam w celu wywoływania tego samego "małego" kodu na innych podstrona gdyż chcę zrobić do tego opcję edytowania,usuwania więc kod zajął by kilka linijek.....

Kod jest taki obszerny i beznadziejny, że nie ma miejsca na wklejenie go.


2)Fragment kodu


  1. echo '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
  2. <thead>
  3. <tr align="center">
  4. <th>Item Number</th>
  5. <th>Item Name</th>
  6. <th>Stats</th>
  7. <th>Socketed</th>
  8. <th>Price</th>
  9. <th>Seller</th>
  10. <th>Status</th>
  11. </tr>
  12. </thead>
  13. <tbody>';
  14. while($row = mysql_fetch_array($result))
  15. {
  16. if ($row['Strength'] == '0') { echo '';} else { $dataStrength ='<li class=d3-color-blue><p style="color:#6969FF;"><span class=value>+ '.$row['Strength'].'</span> Strength</p></li>'; }
  17. if ($row['Dexterity'] == '0') { echo '';} else { $dataDexterity = '<li class=d3-color-blue><p style="color:#6969FF;"><span class=value>+ '.$row['Dexterity'].'</span> Dexterity</p></li>'; }
  18. ..............


I tak dalej do każdego pola ze schematu SQL

a potem wywołane to jest

  1. $attributedump = '
  2. '.@$dataStrength.' '.@$dataDexterity.' '.@$dataIntelligence.' '.@$dataVitality.' '.@$dataRArcane.'
  3. '.@$dataLok.' '.@$dataLph.' '.@$dataLphg.' '.@$dataLifesteal.' '.@$dataAdditional.'
  4. '.@$dataLps.' '.@$dataICold.' '.@$dataIFire.' '.@$dataIHoly.' '.@$dataIPoison.' '.@$dataIWeapon.'
  5. '.@$dataAttackSpeed.' '.@$dataIncreasedMinimumDamage.' '.@$dataIncreasedMaximumDamage.'
  6. '.@$dataCHD.' '.@$dataWd.' '.@$dataChc.' '.@$dataBdmgvsel.' '.@$dataCBlind.' '.@$dataCChill.'
  7. '.@$dataCFear.' '.@$dataCFreeze.' '.@$dataCImmobilize.' '.@$dataCKnockback.' '.@$dataCSlow.'
  8. '.@$dataCStun.' '.@$dataCtbleed.' '.@$dataBleeddmg.' '.@$dataGf.' '.@$dataMf.' '.@$dataExp.'
  9. '.@$dataIndestructible.' '.@$dataMovementSpeed.' '.@$dataYardspick.' '.@$dataReducedLevelRequirement.'
  10. '.@$dataSock1.' '.@$dataSock2.' '.@$dataSock3.' '.@$dataResistanceAll.' '.@$dataRCold.' '.@$dataRFire.'
  11. '.@$dataRHoly.' '.@$dataRLightning.' '.@$dataRPhysical.' '.@$dataRPoison.' '.@$dataIncreasedArmor.'
  12. '.@$dataCCred.' '.@$dataMeleeatd.' '.@$dataReducedDamagefeli.' '.@$dataChancetoblockomshield.'
  13. '.@$dataChancetoblockomattr.' '.@$dataBlockAmount.'
  14. ';


Jestem pewien że nie jeden spadł z krzesła smile.gif no ale działa.

Ma to na celu sprawdzenie, że jeżeli jest podana wartość w SQL np. dla pola Strength = 0 to nie pokaże tego pola w $attributedump

Jest to na prawdę bardzo patologicznie pokazane wszystko lecz jestem pewien, że jest to żenada, proszę o pomoc.

Chętnie nauczę się operacji na takich dużych zmiennych itd. Proszę o podpowiedzi.

Ten post edytował Wertas 15.07.2012, 19:00:12
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 15:39